简体   繁体   English

创建后的骨干模型已经具有属性

[英]Backbone model when created already has attributes

In my my application I do something like this to create a new model, 在我的应用程序中,我要做类似的事情来创建一个新模型,

this.model = new App.Models.Organisation; this.model =新的App.Models.Organisation;

The code for the model looks like this, 该模型的代码如下所示,

    '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() {

    }

});

However when log the new model where I expect to see empty attributes I get the following: 但是,当我在希望看到空属性的地方登录新模型时,得到以下信息:

记录模型

Why would teams and projects have a value when the model is newly created? 新创建模型时,为什么团队和项目会有价值?

The teams collections looks like this, 团队收藏看起来像这样,

    '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;
        },

    });

and the projects collection like this, 还有像这样的项目集合

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;
    },

});

I see what's going on now, in your teams collection initialize method you have this line: 我看到了现在发生的事情,在您的团队集合的初始化方法中,您有以下内容:

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

So this is calling set on a collection which is different from calling set on an individual model. 因此,这是在集合上调用set ,这不同于在单个模型上调用set

  • On a collection set treats the first element as an array of models. 在集合set将第一个元素视为模型数组。 You are passing 'members' as the first parameter and this adding a model to the collection with every character in the string as one attribute of that model 您将“成员”作为第一个参数传递,这会将模型添加到集合中,字符串中的每个字符都作为该模型的一个属性

  • On a model, set expects either an attributes hash to be passed or 2 parameters attribute name and value to be passed, and will set the model attributes accordingly. 在模型上, set期望传递一个属性哈希或传递两个参数属性名称和值,并将相应地设置模型属性。

Basically you cannot treat the collection as an individual model. 基本上,您不能将集合视为单个模型。

If you want to keep a reference to the members from the teams collection, why not keeping a reference like this.members = new App.Collections.Users(members) that you can access from other places in the teams collection? 如果要保留对this.members = new App.Collections.Users(members)集合中成员的引用,为什么不保留这样的引用: this.members = new App.Collections.Users(members) ,您可以从this.members = new App.Collections.Users(members)集合中的其他位置访问该引用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM