gpt4 book ai didi

javascript - 主干模型在创建时已经具有属性

转载 作者:行者123 更新时间:2023-12-03 11:23:18 26 4
gpt4 key购买 nike

在我的应用程序中,我做了类似的事情来创建一个新模型,

this.model = new App.Models.Organization;

模型的代码如下所示,

    'use strict'

App.Models.Organisation = Backbone.Model.extend({

urlRoot: "http://" + App.API_ROOT + "/organisations",

defaults: {
//members : new App.Collections.Users
},

initialize: function() {
//Gets
var members = this.get('users');
var projects = this.get('projects');
var teams = this.get('teams');
var clients = this.get('clients');

console.log(members);
console.log(projects);
console.log(teams);
console.log(clients);

//Sets
if(members != undefined) {
this.set('members', App App.Collections.Users(members));
} else {
this.set('members', App App.Collections.Users);
}

if(projects != undefined) {
this.set('projects', new App.Collections.Projects(projects));
} else {
this.set('projects', new App.Collections.Projects);
}

if(teams != undefined) {
this.set('teams', new App.Collections.Teams(teams));
} else {
this.set('teams', new App.Collections.Teams);
}

if(clients != undefined) {
this.set('clients', new App.Collections.Clients(clients));
} else {
this.set('clients', new App.Collections.Clients);
}


},

validate: function() {

}

});

但是,当记录新模型时,我希望看到空属性,我得到以下结果:

logged model

为什么在新创建模型时团队和项目会有值(value)?

团队集合如下所示,

    'use strict'

App.Collections.Teams = Backbone.Collection.extend({

url: 'http://' + Pops.API_ROOT + '/teams',

model: Pops.Models.Team,

initialize: function() {

var members = this.get('members');
this.set('members', new App.Collections.Users(members));

},

search: function(filterValue) {

var matcher = new RegExp(filterValue);
var found_models = this.filter(function(model) {
return matcher.test(model.get('name'));
});

return found_models;
},

});

以及像这样的项目集合,

App.Collections.Projects = Backbone.Collection.extend({

url: 'http://' + App.API_ROOT + '/project',

model: App.Models.Project,

sort_key: "name",

sort_order: 1,

parent_filter: false,

filters: [1,2,3],

initialize:function() {
var pm = this.get('projectmanager');
this.set('project_manager', new App.Models.User(pm));

var sp = this.get('salesperson');
this.set('sales_person', new App.Models.User(sp));

this.sortByField('created_at', 'desc');
},

comparator: function (item1, item2) {

var val1 = item1.get(this.sort_key);
var val2 = item2.get(this.sort_key);
if (typeof (val1) === "string") {
val1 = val1.toLowerCase();
val2 = val2.toString().toLowerCase();
}

var sortValue = val1 > val2 ? 1 : -1;
return sortValue * this.sort_order;

},

sortByField: function(fieldName, orderType) {
this.sort_key = fieldName;
this.sort_order = orderType == "desc" ? -1 : 1;
console.log(this.sort_order);
this.sort();
},

sortStatus: function( filters ) {
this.filters = filters;
this.each(function(project){
project.set('visible', _.contains(filters, parseInt(project.get('status'))));
});
},

myProjects: function() {
this.each(function(project){
if(project.get('user_id') == '1' && project.get('organisation_id') == null) {
project.set('visible', true);
} else {
project.set('visible', false);
}
}, this);
},

status: function( status ) {
if(this.parent_filter == false) {
//Filter all projects on the dashboard
this.each(function(project){
project.get('visible', true);
project.set('visible', project.get('status') == String(status) );
});

} else {
//Filter only projects that are currently visible
this.each(function(project) {
if(project.get('visible')) {
project.set('visible', project.get('status') == String(status) );
}
});

}

},

otherProjects: function() {

this.each(function(project){

if(project.get('organisation_id') != null) {
project.set('visible', true);
} else {
project.set('visible', false);
}

}, this);

},

sortBy: function(filterBy, orderBy) {
this.sortByField(filterBy, orderBy);
this.sort();
},

search: function(filterValue) {

var matcher = new RegExp(filterValue);
var found_models = this.filter(function(model) {
return matcher.test(model.get('name'));
});

return found_models;
},

});

最佳答案

我明白现在发生了什么,在你的团队集合初始化方法中你有这一行:

this.set('members', new App.Collections.Users(members));`

因此,这是在集合上调用 set,这与在单个模型上调用 set 不同。

  • 收藏 set将第一个元素视为模型数组。您将“members”作为第一个参数传递,并将模型添加到集合中,并将字符串中的每个字符作为该模型的一个属性

  • 在模型上,set期望传递一个属性哈希或传递 2 个参数属性名称和值,并将相应地设置模型属性。

基本上,您不能将集合视为单个模型。

如果您想保留对团队集合中成员的引用,为什么不保留像 this.members = new App.Collections.Users(members) 这样您可以从其他地方访问的引用在团队集合中的位置?

关于javascript - 主干模型在创建时已经具有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27019968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com