在Backbone 的view里如何更新model?

发布于 2022-09-01 06:53:53 字数 580 浏览 16 评论 0

需求是在一个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));***

            }
        });

    },

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷︶言冷语的世界 2022-09-08 06:53:53

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

            }
        });

    },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文