Env: Titanium 3.1.3, Alloy 1.2.2. I'm using the following adapter for persistence on the models/collections: https://github.com/viezel/napp.alloy.adapter.restapi
I have an API that has a different URL structure for a collection than it does a single model. Consider the following:
To get a single record: [GET] /files/:id
To get all the files for a user: [GET] /users/:id/files
I have the following schema for "files.js":
exports.definition = { config: { "URL": "https://my.api.here/files", //"debug": 1, "adapter": { "type": "restapi", "collection_name": "files", "idAttribute": "id" } }, extendModel: function(Model) { _.extend(Model.prototype, {}); return Model; }, extendCollection: function(Collection) { _.extend(Collection.prototype, { initialize: function(){ this.url = "http://my.api.here/users/"+this.user_id+"/files"; } }); return Collection; } }What I'm trying to do in the above is override the collection initialize method to change the URL structure for the collection. I then call this accordingly:
var currentUserFiles = Alloy.createCollection("files", {user_id:"12345"}); currentUserDrawers.fetch({ success: function(files){ console.log("Woo! Got the user's files!"); console.log(JSON.stringify(files.models)); }, error: function(){ console.log("Nope"); } });This doesn't work. The fetch() method just continues to try to call /files. I've tried setting url as a property on the collection after it's created, that also don't work.
Ideally, I'd like to do this for both local instances as well as the singleton version of the collection.
So - the question is: can I utilize a different URL for a collection than I do for a model? Obviously, I don't want to just call /files and sort/filter client-side - that'd be a nightmare with a lot of records. What am I missing here?