Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 澄清-处理Backbone.js中的附加信息_Javascript_Jquery_Backbone.js - Fatal编程技术网

Javascript 澄清-处理Backbone.js中的附加信息

Javascript 澄清-处理Backbone.js中的附加信息,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,我对主干网还比较陌生,我正在做一个项目,这个项目对主干网来说可能并不理想,但我还是在尝试!我没有使用RESTful API,所以我的所有数据都是在页面加载时获取的,也就是说,它根本不会更新。数据作为单一的JSON块发送 我已经建立了一个新的模型CookingItem,它为JSON中的每个项目创建,然后添加到CookingItemList集合中,这很好。但是,我需要将有关数据集的信息存储在某个位置,因此我创建了另一个“属性模型”,用于存储此信息(状态信息、开始项等)。此属性模型还将有几个方法来操作

我对主干网还比较陌生,我正在做一个项目,这个项目对主干网来说可能并不理想,但我还是在尝试!我没有使用RESTful API,所以我的所有数据都是在页面加载时获取的,也就是说,它根本不会更新。数据作为单一的JSON块发送

我已经建立了一个新的模型
CookingItem
,它为JSON中的每个项目创建,然后添加到
CookingItemList
集合中,这很好。但是,我需要将有关数据集的信息存储在某个位置,因此我创建了另一个“属性模型”,用于存储此信息(状态信息、开始项等)。此属性模型还将有几个方法来操作数据。。然后从视图中调用它们(这样可以吗?)

这很好,但我只是想确定我所做的被认为是一种好的做事方式。我相信可能有更好的方法来处理
属性
模型

// Models
var CookingItem = Backbone.Model.extend(),

CookingProperties = Backbone.Model.extend({
    defaults:{
        startItem: 'anything',
        currentItems:[]
    },
    setStartItem:function (val) {
        // Code Here to edit this models properties
    },
    addRemoveItem:function (val) {
        // Code Here to edit this models properties
    }
}),

// Collection
CookingItemList = Backbone.Collection.extend({
    model:CookingItem,
    url:function () {
        return "json.js";
    }
}),

// View
CookingView = Backbone.View.extend({
    el:'.page',
    events:{
        // Buttons in the UI
        'click .link1':function () {
            this.cookingProperties.addRemoveItem('item name');
        },
        'click .link2':function () {
            this.cookingProperties.addRemoveItem('item name');
        }
    },
    initialize:function () {
        // Scope
        var _this = this;
        // Instantiate
        this.cookingItemList = new CookingItemList();  // PlayListItems Collection.
        this.cookingProperties = new CookingProperties(); // Properties Model.
        // Bindings
        this.cookingProperties.on('change:startItem', function () {
            _this.customMethod1();
            _this.customMethod2();
        });
        // Render the view
        this.render();
    },
    render:function () {
        var _this = this;
        this.cookingItemList.fetch({
            success:function () {
                _this.cookingProperties.setStartItem('item');
                _this.customMethod1();
            }
        });
        return this;
    },
    customMethod1:function () {
        // Do something
    },
    customMethod2:function () {
        // Do something
    }
}),

// Start
cookingView = new CookingView({
    collection:CookingItemList
});

看看这个,你可能想考虑把这个贴在上面。