Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone - Possible to get the collection from a model

I'm wondering if there's a way to get a reference to a collection from one of its models. For instance, if any of the people in the collection below are somehow aware of belonging to a collection, or multiple collections. Fiddle

(function() {
window.App = {
    Models: {},
    Views: {},
    Collections: {}
};

App.Models.Person = Backbone.Model.extend({
    defaults: {
        name: 'John',
        phone: '555-555-5555'
    }
});

App.Views.Person = Backbone.View.extend({
    tagName: 'li',

    template: _.template("<%= name %> -- <%= phone %>"),

    render: function(){
        var template = this.template( this.model.toJSON() );

        this.$el.html( template );

        return this;
    }
});

App.Collections.People = Backbone.Collection.extend({
    model: App.Models.Person 
});

App.Views.People = Backbone.View.extend({
    tagName: 'ul',

    add: function(person){
        var personView = new App.Views.Person({ model: person });

        this.$el.append( personView.render().el );

        return this;
    },

    render: function() {
        this.collection.each(this.add, this);

        return this;
    }
});


})();

var peeps = [ { name: 'Mary' }, { name: 'David' }, { name: 'Tiffany' } ];

var people = new App.Collections.People(peeps);

var peopleView = new App.Views.People({ collection: people });

peopleView.render().$el.appendTo('body');
like image 313
Nick Brown Avatar asked Apr 12 '13 04:04

Nick Brown


People also ask

What is a model backbone?

Model. Models are the heart of any JavaScript application, containing the interactive data as well as a large part of the logic surrounding it: conversions, validations, computed properties, and access control. You extend Backbone.

What is Backbone JS used for?

BackboneJS is a lightweight JavaScript library that allows to develop and structure the client side applications that run in a web browser. It offers MVC framework which abstracts data into models, DOM into views and bind these two using events.

What is Backbone in programming?

Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).

How can we get the attribute value of a model in Backbone JS?

js Get model is used to get the value of an attribute on a model. Syntax: model. get(attribute)


1 Answers

Each model has a property called collection. In your fiddle, adding console.log(people.models[0].collection) will print out the collection.

Looking through the source code, it looks like this is what's used to do things like remove a model from a collection when the model's destroy() method is called.

Update: see this updated fiddle which creates three person models and two collections. It prints them to the console. It looks like model.collection only refers to the first collection the person was added to, not the second.

like image 126
Cianan Sims Avatar answered Oct 01 '22 13:10

Cianan Sims