I'm using Backbone.js and StackMob for a mobile app, and have run into a strange problem that I can't get my head around. According to the StackMob docs, StackMob.Model extends Backbone.Model, and thus has access to the same methods, and should work the same way. As I understand it, it's a simple swap to make.
Here's the situation:
I have a collection that's being populated by an API call to FourSquare, which is being rendered out into the view as a list. Each model in the list is rendered with a data-id attribute:
$("ul#resultsList").append("<li class='result' data-id='" + place.get('id') + "'><span class='listingName'>" + place.get("name") + "</span><span class='listingDetail'>" + place.get("location")["address"] + "</span><span class='listingDetail'>" + place.category + "</span></li>");
(The above code is what's looped through to build the list).
The list shows up fine, and everything on that end works as it should. Then, I have the following attached to the click event on each list item:
saveItem: function(e) {
window.item = $(e.currentTarget).data("id");
window.newplace = this.collection.get(window.item);
console.log("New place: "+ window.newplace); //shows the model in Backbone, undefined in StackMob
App.navigate("#add", {
trigger: true
});
},
This code basically looks at the item clicked, goes and gets the ID, then looks up that model in the collection based on the ID and stores it into window.newplace.
Then, the app navigates over to #add, which instantiates the view with window.newplace as the model:
addItem: function() {
if (!window.newplace) {
window.newplace = new Place({});
}
this.changePage(new AddItemView({model:window.newplace}), "slide");
},
Here's the problem:
This works exactly as it should when the Place model is set up as a Backbone.Model. As soon as I switch it to StackMob.Model, it shows window.newplace as undefined.
I can't imagine why switching these should make any bit of difference, but it seemingly does.
Just for the sake of full clarification, here's the model when using Stackmob.Model:
window.Place = StackMob.Model.extend({
...
});
Any ideas? If it didn't work at all, I'd chalk it up to some really stupid thing, but since it works fine extending Backbone, but not StackMob, I'm really confused.
Thanks!