【问题标题】:Backbone.js | How can I store elements of a view for retrieval later?Backbone.js |如何存储视图元素以供以后检索?
【发布时间】:2010-12-27 22:55:17
【问题描述】:

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

将模型添加到集合时遇到问题。我已将视图方法绑定到应该更新视图的集合的添加事件。这是尽可能多地删除不相关代码的代码。

(注意:为简洁起见,我删除了局部范围 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);
});  

我已经用 cmets 标记了有问题的区域。根据Backbone Documentation

如果页面中包含 jQuery 或 Zepto,则每个视图都有一个 $ 函数,用于运行视图元素范围内的查询。

那么,如果我在渲染方法中设置了this.menuList = this.$('#menuList');,为什么我不能在addMenuItem 方法中访问this.menuList?我在顶部链接到的演示就是这样。另外,如果我将 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 jquery view undefined backbone.js


    【解决方案1】:

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

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

    有了这个:

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

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

    _.bindAll(this, 'addMenuItem');
    

    【讨论】:

    • _.bindAll(this, 'addMenuItem'); 成功了。 underscore.js 文档为我解释了它。谢谢!
    【解决方案2】:

    可能在调用 render 方法之前调用了 addMenuItem 方法,因此 addMenuItem 中缺少 menuList 的定义。

    为什么不将 this.menuList = this.$('#menuList'); 推送到 initialize 方法并尝试一下?

    【讨论】:

    • 试一试。仍然未定义。请注意,博客教程的所有内容都按与我的示例相同的顺序列出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2013-06-23
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多