在Backbone 的view里如何更新model?

需求是在一个view里面,当用户输入一个数据,后台查询到这个数据后,更新当前view的model,marionette就会自动rerender 当前view。 现在问题是,如何将查询的数据更新到当前的model? 无法直接调用this.model, 如何才能用用model对象?

    fetchRoute:function(e){

        var inputstr = $(".code_input").val();
        var fetchingroutes = app.request("entities:routes",{c:inputstr});
        $.when(fetchingroutes).done(function(routes){
            if(routes.length >= 1){
                console.log('fetch a route'+JSON.stringify(this.model));
                ***///this.model.set(routes.at(0));***

            }
        });

    },
阅读 4.4k
1 个回答

fetchRoute 里面缓存 thismodel 即可。

    fetchRoute:function(e){
        var view = this; // 缓存 this

        var inputstr = $(".code_input").val();
        var fetchingroutes = app.request("entities:routes",{c:inputstr});
        $.when(fetchingroutes).done(function(routes){
            if(routes.length >= 1){
                console.log('fetch a route'+JSON.stringify(view.model));
                view.model.set(routes.at(0));

            }
        });

    },
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进