模型ID未设置为backbone.js

时间:2015-03-08 22:58:31

标签: javascript backbone.js models

userProjects: function(customerid) {

    this.chat();

    App.core.vent.trigger('app:log', 'Controller: "userProjects" route hit.');
    window.customerTransactions = new CustomerTransactionsCollection([], {id:customerid});
    customerTransactions.fetch({
        success: function(transactions) {
            App.data.transactions = transactions;
            var userProjectsView = new UserProjectsView({ collection: customerTransactions });
            $('.dashboard .container').html(userProjectsView.render().$el);
        }
    });

    window.App.router.navigate('/customer/'+customerid);

}

该集合看起来有点像

[
    {"id":"czy3m4","status":"authorized"},
    {"id":"3sjkn8","status":"authorized"},
]

然后当我记录App.data.transactions.get('czy3m4')时,它是未定义的。来自客户端的cid,但模型本身没有id,在我的其他集合中,id就在那里。

module.exports = CustomerTransactionModel = Backbone.Model.extend({
    idAttribute: 'id'
});

enter image description here

解决方法

作为一种解决方法,我正在做这个

userProject: function(projectid) {

    console.log(projectid);

    this.chat();

    var thisModel;
    App.data.transactions.each(function(model) {
        if(model.get('id') === projectid) {
            thisModel = model;
        }
    });

    App.core.vent.trigger('app:log', 'Controller: "userProject" route hit.');
    var userProjectView = new UserProjectView({ model: thisModel });
    $('.dashboard .container').html(userProjectView.render().$el);

    window.App.router.navigate('/customer/'+customerid+'/'+projectid);

}

0 个答案:

没有答案