Hi, I'm developing an application in Titanium and when I set the Backbone SQL adapter's "idAttribute", I get some weird save behaviour when trying to update a model.
Model and adapter:
exports.definition = { config: { columns: { "user_id": "text", "first_name": "text", "last_name": "text" }, adapter: { type: "sql", collection_name: "FBfriend", idAttribute: "user_id" } }, ...(Note: "user_id": "text", idAttribute: "user_id")
_Initial population code:_
var fr = Alloy.createModel('FBfriend'); fr.fetch({id: "123"}); Ti.API.info("fetched "+JSON.stringify(fr)); Ti.API.info("first_name "+fr.get("first_name")); fr.set("user_id", "123"); fr.set("first_name", "Alice"); fr.save(); Ti.API.info("after "+JSON.stringify(fr)); Ti.API.info("first_name "+fr.get("first_name")); var friends = Alloy.createCollection("FBfriend"); friends.fetch(); Ti.API.info("all "+ JSON.stringify(friends));_output:_
Application started [INFO] : fetched {} [INFO] : first_name undefined [INFO] : after {"user_id":"123","first_name":"Alice"} [INFO] : first_name Alice [INFO] : all [{"user_id":"123","first_name":"Alice","last_name":null}]
So, we restart the emulator and attempt a model update (change 'Alice' to 'Wendy'). _code:_
var fr = Alloy.createModel('FBfriend'); fr.fetch({id: "123"}); Ti.API.info("fetched "+JSON.stringify(fr)); Ti.API.info("first_name "+fr.get("first_name")); fr.set("first_name", "Wendy"); fr.save(); Ti.API.info("after "+JSON.stringify(fr)); Ti.API.info("first_name "+fr.get("first_name")); var friends = Alloy.createCollection("FBfriend"); friends.fetch(); Ti.API.info("all "+ JSON.stringify(friends));_output:_
[INFO] : Application started [INFO] : fetched {"user_id":"123","first_name":"Alice","last_name":null} [INFO] : first_name Alice [INFO] : after {"user_id":"123","first_name":"Wendy","last_name":null} [INFO] : first_name Wendy [INFO] : all [{"user_id":"123","first_name":"Alice","last_name":null}](When we retrieve the FBfriend collection, the model is still 'Alice", not 'Wendy')
But if we restart the emulator and fetch the model again (to see what it looks like), we get to the really weird part _code:_
var fr = Alloy.createModel('FBfriend'); fr.fetch({id: "123"}); Ti.API.info("fetched "+JSON.stringify(fr)); Ti.API.info("first_name "+fr.get("first_name")); var friends = Alloy.createCollection("FBfriend"); friends.fetch(); Ti.API.info("all "+ JSON.stringify(friends));_output:_
[INFO] : Application started [INFO] : fetched {"0":{"user_id":"123","first_name":"Alice","last_name":null},"1":{"user_id":"123","first_name":"Wendy","last_name":null}} [INFO] : first_name undefined [INFO] : all [{"user_id":"123","first_name":"Alice","last_name":null}]The fetched model is an array. And in the collection of all FBfriend objects we see it's a normal model, but it's the old one.
I'm not sure what's going on here. I expected the model to just update the first_name attribute in the DB, not spawn a sequence. Anyone?