Backbone collections to work with Titanium

You must Login before you can answer or comment on any questions.

This is for a mobile app targeting the 5.1 version of the iPhone sdk in the simulator with Titanium SDK version 2.1.1 (07/27/12 14:01 0fd84a2).

I am attempting to get Backbone.js 0.9.2 to work with Titanium. I have modified the Backbone.sync function to use the Titanium HTTPClient and the data comes back fine.

It looks like there is an issue when the collection is built in the reset function where it adds models to internal hashes for lookup. My guess is that this is causing a memory issue because Titanium crashes hard without a message when this line is run:

source:

this._byCid[model.cid] = model;
if (model.id != null) this._byId[model.id] = model; // this will also cause the halt
Is there anyone that has gotten backbone and titanium playing together recently that may have some insight on what is wrong/how to fix this?

Thanks, Robert

3 Answers

Accepted Answer

Code seems to be working fine for me.... don't know what you had the parse method in there for?

// load initial data
  var tweets = new TweetCollection();
  tweets.fetch({
    error : function(collection, response) {
      console.log('error');
    },
    success : function(collection, response) {
      console.log('success');
 
      // accessing the collection either way causes a crash
      // [INFO] Application has exited from Simulator
      for (var i in tweets.models) {
        console.log(tweets.models[i].get("user").screen_name + " " + tweets.models[i].get("text"));
      }
      //console.log(tweets);
    }
  });

— answered 10 months ago by Aaron Saunders
answer permalink
4 Comments
  • The Parse was for debugging. What you have works, but a bunch of these methods fail.
    Can you try these in the callback and see if they work for you:

    console.log(tweets)

    console.log(tweets.at(0))

    console.log(tweets.first())

    console.log(get(id))

    Those all fail for me. Also, the functions exist on the object, they simply crash the simulator when I access them:

    console.log(typeof tweets.at === 'function'); // 1, it's there

    console.log(tweets.at(0)); // crash

    Can you reproduce that?

    — commented 10 months ago by Robert Schooley

  • console.log(get(id)) should be console.log(tweets.get(id)), and the id I grabbed from the array

    — commented 10 months ago by Robert Schooley

  • you need to stop using console :-)

    try this console.log(JSON.stringify(tweets.at(0))); it works fine, the problem is that console.log cannot parse the JSON properly so it is crashing

    — commented 10 months ago by Aaron Saunders

  • Show 1 more comment

I created a simple one view app with the backbone code I have now. I pointed it to a random twitter stream for data, and zipped the whole project.

Thanks for any help figuring out what is wrong.

zip file

Your Answer

Think you can help? Login to answer this question!