Backbone model when created already has attributes

74 views Asked by At

In my my application I do something like this to create a new model,

this.model = new 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:

logged model

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

});
1

There are 1 answers

0
Daniel J.G. On

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.

  • On a collection set treats the first element as an array of models. 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.

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?