Javascript 未设置模型ID backbone.js
集合看起来有点像这样Javascript 未设置模型ID backbone.js,javascript,backbone.js,models,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}); customerT
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);
}
然后当我记录App.data.transactions.get('czy3m4')时,它是未定义的。有来自客户端的cid
,但没有来自模型本身的id
,在我的其他收藏中,id就在那里
[
{"id":"czy3m4","status":"authorized"},
{"id":"3sjkn8","status":"authorized"},
]
解决方法
作为解决办法,我正在这样做
module.exports = CustomerTransactionModel = Backbone.Model.extend({
idAttribute: 'id'
});
这个模型看起来像什么?集合如何?模型/集合的主干配置还是json结构?它与我的其他模型/集合几乎相同,但由于某种原因,当我记录集合并查看可以查看模型的cid的模型时,模型的id不可用,这可能是因为我如何提供数据,我正在重写服务器。您的模型或集合中是否有任何
parse
方法?您的模型或系列中还有什么其他内容吗?可能是idAttribute
设置?这真的是JSON看起来的样子吗?所以主要的区别是为了检索项目,我必须通过id在api中找到客户,然后通过id找到项目,因此路径看起来像/api/customer/12345/project/0987
,为了获取项目,我需要先找到合适的客户,因此,在我的主干集合中,我自定义url路由url:function(){return'/api/customer/'+this.id;},
为了正确获取,我执行了window.customerTransactions=new CustomerTransactionsCollection([],{id:customerid})
总体目标是下拉特定的项目模型,以便在通过customer然后customers projects collection然后是特定模型导航到它之后,我可以直接使用它。
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);
}