Guest User

Untitled

a guest
Dec 13th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*THE CINEMA MODEL*/
  2.  
  3.  CinemaModel = Backbone.RelationalModel.extend({
  4.             url:"scripts/data/movies1.json",
  5.             name:'',
  6.             idAttribute: 'id',
  7.             relations: [
  8.                 {
  9.                     type: Backbone.HasMany,
  10.                     key: 'movies',
  11.                     relatedModel: 'MovieModel',
  12.                     includeInJSON: Backbone.Model.prototype.id,
  13.                     reverseRelation: {
  14.                         /*type: Backbone.HasOne,*/
  15.                         key: 'CinemaModel'
  16.                     }
  17.                     //collectionType:MoviesCollection
  18.                 }
  19.  
  20.             ],
  21.  
  22.             initialize: function(){
  23.                 //alert("Cinema Model initialize")
  24.             }
  25.         });
  26.  
  27. /*THE MOVIE MODEL*/
  28.  
  29.    MovieModel = Backbone.RelationalModel.extend({
  30.             name:'',
  31.             idAttribute: 'id',
  32.             initialize: function(){
  33.                 alert("Movie Model initialized")
  34.                 },
  35.            });
  36.  
  37. /*THE MOVIE COLLECTION*/
  38.  
  39.   MoviesCollection = Backbone.Collection.extend({
  40.             url: "scripts/data/movies1.json",
  41.  
  42.             model: CinemaModel,
  43.  
  44.             initialize: function(){
  45.                    console.log("Movies Collection initialize")
  46.                 }
  47.  
  48.           });
  49.  
  50. /*The IMPLEMENTATION*/
  51.  moviesCollection = new MoviesCollection();
  52.         moviesCollection.fetch({success:function (collection,response) {
  53.             console.log("Yes! ");
  54.         },
  55.             error:function (collection, response) {
  56.                 console.log("NO! ");
  57.             }
  58.         });
  59.    
  60.         moviesCollection.models[0].get('movies')[0];
  61.  
  62.  
  63. /*The JSON*/
  64.  
  65. [
  66.  {
  67.                 "id":"1.1",
  68.                 "name":"Barfi"
  69.             },
  70.             {
  71.                 "id":"1.2",
  72.                 "name":"Amar Akbar Barfi"
  73.  
  74.             },
  75.  {
  76.                 "id":"3.1",
  77.                 "name":"Mangal Pandey"
  78.             },
  79.             {
  80.                 "id":"3.2",
  81.                 "name":"3 idiots"
  82.             }
  83. ]
  84.  
  85. [
  86.     {
  87.         "id":"1",
  88.         "name":"Cinema 1",
  89.         "movies":["1.1","1.2"]
  90.     },
  91.     {
  92.         "id":"2",
  93.         "name":"Cinema 2",
  94.         "movies":[]
  95.     },
  96.     {
  97.         "id":"3",
  98.         "name":"Cinema 3",
  99.         "movies":["3.1","3.2"]
  100.     }
  101. ]
Add Comment
Please, Sign In to add comment