javascript - Backbone .js |我如何存储 View 的元素以供以后检索?

标签 javascript jquery view undefined backbone.js

这是我第一次尝试使用 Backbone.js,所以我决定制作一个简单的测试应用程序来模拟带有菜单项的餐厅菜单。我跟着this cool blog post from andyet.net一起.

我在将模型添加到集合时遇到问题。我已经将 View 方法绑定(bind)到集合的添加事件,该事件应该更新 View 。这是尽可能多地删除无关代码的代码。

(注意:为了简洁起见,我删除了局部作用域 var 声明和闭包之类的东西。下面仍然有点长,我知道这很烦人,但应该是非常简单易懂):

MenuItem = Backbone.Model.extend({
    // initialize functions removed for brevity
});
Menu = Backbone.Model.extend({
    // initialize functions removed for brevity
    // MenuSelection property is defined here.
});
MenuSelection = Backbone.Collection.extend({ model: MenuItem });
MenuItemView = Backbone.View.extend({
    // render template
});

/**
 * This is unaltered from the code.  The error occurs here.
 */
RestaurantAppView = Backbone.View.extend({
    addMenuItem : function (MenuItem) {
        var view = new MenuItemView({ model : MenuItem });
        this.menuList.append(view.render().el);
        // ERROR occurs here.  error: "this.menuList is undefined"
    },
    initialize : function () {
        this.model.MenuSelection.bind('add', this.addMenuItem);
    },
    render : function () {
        $(this.el).html(ich.app(this.model.toJSON()));
        this.menuList = this.$('#menuList'); // IT IS DEFINED HERE
        return this;
    }
});

/**
 * Everything after this I left in just-in-case you need to see it.
 */
RestaurantAppController = {
    init : function (spec) {
        this.config = { connect : true };
        _.extend(this.config, spec);
        this.model = new Menu({
           name : this.config.name,
        });
        this.view = new RestaurantAppView({ model : this.model });
        return this;
    }
};
$(function() {
    // simulating ajax JSON response.
    var json = {
        Menu : {
            name : 'Appetizers',
            MenuItem : [
                {
                    name : 'toast',
                    description : 'very taosty',
                    price : '$20.00'
                },
                {
                    name : 'jam',
                    description : 'very jammy',
                    price : '$10.00'
                },
                {
                    name : 'butter',
                    description : 'very buttery',
                    price : '$26.00'
                }
            ]
        }
    };
    window.app = RestaurantAppController.init({
        name : json.Menu.name
    });
    $('body').append(app.view.render().el);
    app.model.MenuSelection.add(json.Menu.MenuItem);
});  

我已经用评论标记了有问题的区域。据Backbone Documentation :

If jQuery or Zepto is included on the page, each view has a $ function that runs queries scoped within the view's element.

所以,如果我在渲染方法中设置 this.menuList = this.$('#menuList');,为什么我不能访问 this.menuListaddMenuItem 方法中?我在顶部链接到的演示就是这样做的。此外,如果我将 this.menuList 换成 jQuery 选择器,如下所示:

addMenuItem : function (MenuItem) {
    var view = new MenuItemView({ model : MenuItem });
    $('#menuList').append(view.render().el);
}

一切正常。但我不希望在每次执行addMenuItem 时都重新选择菜单列表ID。 RightWayTM 是在渲染后缓存它。

另请注意:我认为问题可能出在 ICanHaz 模板返回速度不够快,但是 this.menuList 将是一个空数组,而不是 undefined,所以不是这样的。

最佳答案

您遇到了 JavaScript 的第一个难题——动态作用域的 this 关键字。当您将 this.addMenuItem 作为引用而不绑定(bind)它时,addMenuItem 函数将失去其 this 的概念。有两种简单的方法可以在您的代码中修复它,或者替换此行:

this.model.MenuSelection.bind('add', this.addMenuItem);

有了这个:

this.model.MenuSelection.bind('add', _.bind(this.addMenuItem, this));

或者将这一行添加到初始化函数的顶部,这将有效地完成同样的事情:

_.bindAll(this, 'addMenuItem');

关于javascript - Backbone .js |我如何存储 View 的元素以供以后检索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4542433/

相关文章:

javascript - 连接日期和时间值

javascript - 每篇文章都有一个单独的链接(bootstrap modal、django、jQuery)

javascript - 使用 JQuery 在 JS Reveal 演示文稿中定位内容

sql - 在子查询中排序

javascript - angular.js 和 zurb foundation 显示模态

javascript - 以一定速度滚动时,Masonry 和 Infinite Scroll 会破坏布局

javascript - 是否可以在 CouchDB 验证功能中向文档添加字段?

jquery - 本地主机上的 laravel 5.3 中 ajax post 请求中的 CSRF token 不匹配异常

html - 使用 ajax 将数据从 Codeigniter Controller 传递到 div

wpf - 要查看的 MVVM 模型 View