Losing context with parse.com javascript SDK -
so goal of i'm doing store array of objects inside object literal later reference. losing context(if right terminology use here) in place confusing me. here code:
huntobject = { // data.hunts gives collection data: {}, fetchcollec: function(){ var self = this; var huntobj = new parse.query(huntobject); huntobj.find({ success: function(results){ var hunts = []; for(i in results){ hunts.push(i); } console.log(self); //here self references huntobject self.data = hunts; }, error: function(e){ console.log(e.message); } }); console.log(self);// here self references huntobject console.log(self.data); // empty so in both console.log statements correct context want referenced , in last log call self can see in console data object has array of objects in it. try reference array empty object. tried assigning array in different ways self.data.array = hunts. tried set data method this.
data: (function(){ return { array: [] } }()); i think maybe understanding of how context changes in different situations pretty weak nice solve original goal more important understand context better , why implementation failing in instance?
this isn't scope or context issue, you're handling self variable.
my guess parse.query asynchronous. , you'd have console.log within success function; doing in code after call parse.query calls (before query completes).
so:
huntobj.find({ success: function (results) { var hunts = []; (i in results) { hunts.push(i); } console.log(self); //here self references huntobject self.data = hunts; // *********** move these here console.log(self); // here self references huntobje console.log(self.data); // not empty anymore }, error: function (e) { console.log(e.message); } }); // code here runs after you've *started* query, // before *completes*
Comments
Post a Comment