首页 > 在Backbone 的view里如何更新model?

在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));***

            }
        });

    },

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));

            }
        });

    },
【热门文章】
【热门文章】