backbone.js - Connection between model and collection in backbone and parse.com -


i'm trying connect model , collection using parse.com i'm confused. i'm tring fetch collection using backbone , javascript api parse.com compare error:post https://api.parse.com/1/classes 404 (not found).

model:

    var person = backbone.model.extend({     defaults:{           },     initialize:function(){           console.log("inperson");           this.validate();           this.send();       },    validate:function(){           console.log("validate");       },        send:function(){            var user = new parse.user();            user.set("username", this.get("username"));            user.set("password", this.get("password"));            user.set("email", this.get("email"));               user.signup(null, {            success: function(user) { // hooray! let them use app now.             },            error: function(user, error) { // show error message somewhere , let user try again.            alert("error: " + error.code + " " + error.message);            }  });         }       });       return person;   }); 

collection:

  var usercollection = parse.collection.extend({   model:person,      initialize:function(){      }    });       return usercollection;    }); 

and view call colletion , fetch:

var homeview = backbone.view.extend({  template: handlebars.compile(template),   events: {     },    initialize: function() {        console.log("inhomeview");        var amici = new usercollection();     amici.fetch({   success: function(collection) {    amici.each(function(object) {   console.warn(object); }); }, error: function(amici, error) { // collection not retrieved. }  });       },     render: function() {       }     });   return homeview;  }); 

cant swap backbone collection , model parse's ones? (you used parse type of collection, not model!) try switch backbone model parse.object .

step step below:

first of lets create new app on parse.com, mine called funkyappartments. insert script tag loading parse javascript lib index.html or whathever:

<script src="http://www.parsecdn.com/js/parse-1.5.0.min.js"></script> 

switch backbone model , collection use parse types instead (and rename fetch method if have extended backbones, since not want overide 1 of parse):

  //var appartment = backbone.model.extend(); backbone wo. parse.com   var appartment = parse.object.extend("appartment");    //var appartments = backbone.collection.extend({ backbone wo. parse.com   var appartments = parse.collection.extend({     model: appartment,     loadappartments: function(callback){       debugger;       this.query = new parse.query(appartment);       this.fetch();     }    }); 

i added debugger tag in load appartments developer tools breaks in middle of controller, here have access appartment private type of controller, hence can store data on parse server , verify pasting below in developer tools console.

var testappartment = new appartment(); testappartment.save({name: "foobars"}).then(function(object) {   alert("yay! worked"); }); 

yei, data shows in parse.com ui app added there. , more importantly shows in our frontend. easy!

update: problems w backbone 1.2.1, marionette 2.4.2, underscore 1.8.3

i noticed had been using old versions of marionette, backbone , underscore.js. initial update appeared break application.

after research found parse part did not return objects render. hence changed collection type extension of: backbone.collection instead of parse.collection.

i had override query method, since objects not save on correct id, updating object resulted in new object being added instead of old 1 being updated.

var apartment = parse.object.extend('appartment');  var apartments = backbone.collection.extend({   model: apartment,   query: new parse.query(apartment),   initialize: function(){     myapp.vent.on('search:param', function(param){self.search(param); });     var self = this;     this.query.find({         success: function(results){             self.reset();             results.foreach(function(result){               result.attributes.id__ = result.id               var ap = new apartment(result.attributes);               self.add(ap);             });           }       });   } }); 

i added attribute: id__ hold parse id (naming id did not work since backbone interfered it, making disappear). in saving model parse utilized id__ id in save call:

  var apartmenteditview = backbone.marionette.itemview.extend({     template: "#apartment-edit-template",     classname: "apartmentdetail",     events: {       "click .store": "storeedit",       "click .close": "closeedit"     },     storeedit: function(){       var pricenum = number($('#price_field').val().replace(/\s/g, ''));        this.model.set({         id: this.model.attributes.id__,         name:$('#name_field').val(),         price:pricenum,         description:$('#desc_field').val(),         url:$('#url_field').val()       });       this.model.save();       this.closeedit();     },     closeedit: function(){       var detailview = new apartmentdetailview({model: this.model});       myapp.subletting.layout.details.show(detailview);     }   }); 

now object updated correctly in database.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -