Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*THE CINEMA MODEL*/
- CinemaModel = Backbone.RelationalModel.extend({
- url:"scripts/data/movies1.json",
- name:'',
- idAttribute: 'id',
- relations: [
- {
- type: Backbone.HasMany,
- key: 'movies',
- relatedModel: 'MovieModel',
- includeInJSON: Backbone.Model.prototype.id,
- reverseRelation: {
- /*type: Backbone.HasOne,*/
- key: 'CinemaModel'
- }
- //collectionType:MoviesCollection
- }
- ],
- initialize: function(){
- //alert("Cinema Model initialize")
- }
- });
- /*THE MOVIE MODEL*/
- MovieModel = Backbone.RelationalModel.extend({
- name:'',
- idAttribute: 'id',
- initialize: function(){
- alert("Movie Model initialized")
- },
- });
- /*THE MOVIE COLLECTION*/
- MoviesCollection = Backbone.Collection.extend({
- url: "scripts/data/movies1.json",
- model: CinemaModel,
- initialize: function(){
- console.log("Movies Collection initialize")
- }
- });
- /*The IMPLEMENTATION*/
- moviesCollection = new MoviesCollection();
- moviesCollection.fetch({success:function (collection,response) {
- console.log("Yes! ");
- },
- error:function (collection, response) {
- console.log("NO! ");
- }
- });
- moviesCollection.models[0].get('movies')[0];
- /*The JSON*/
- [
- {
- "id":"1.1",
- "name":"Barfi"
- },
- {
- "id":"1.2",
- "name":"Amar Akbar Barfi"
- },
- {
- "id":"3.1",
- "name":"Mangal Pandey"
- },
- {
- "id":"3.2",
- "name":"3 idiots"
- }
- ]
- [
- {
- "id":"1",
- "name":"Cinema 1",
- "movies":["1.1","1.2"]
- },
- {
- "id":"2",
- "name":"Cinema 2",
- "movies":[]
- },
- {
- "id":"3",
- "name":"Cinema 3",
- "movies":["3.1","3.2"]
- }
- ]
Add Comment
Please, Sign In to add comment