Titanium JIRA Archive
Appcelerator Community (AC)

[AC-2389] Alloy: Model Fetch by ID does not work on instances (singletons)

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionInvalid
Resolution Date2013-06-24T13:16:39.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsAlloy
Labelsalloy, model, preferences
ReporterSam Cleaver
AssigneeShak Hossain
Created2013-06-22T01:50:08.000+0000
Updated2016-03-08T07:41:25.000+0000

Description

I have a preferences model which stores app preferences/settings; it uses the preferences sync adapter for this purpose. However when attempting to retrieve a result using the fetch({id: }) method, undefined is always returned. This makes the use of instances (singletons) impossible, the workaround is to use an instance of a Model initiated with the ID desired. For example: Works (returns the object):
 
var genretest = Alloy.createModel('preferences', {id: "genreversion"});
genretest.fetch();
Doesn't work (returns undefined):
 
var genretest2 = Alloy.Models.instance('preferences');
genretest2.fetch({id: "genreversion"});
Also doesn't work (something wrong with passing ID via fetch?)
 
var genretest = Alloy.createModel('preferences');
genretest.fetch({id: "genreversion"});
Obviously this is a performance issue as it means that singletons cannot be used throughout the app if a specific ID is desired (unless I've misinterpreted something). I've attached my preferences.js model to assist in validation.

Attachments

FileDateSize
preferences.js2013-06-22T01:50:08.000+0000680

Comments

  1. Sam Cleaver 2013-06-22

    Through some preliminary testing modifying the Alloy source it seems that if the ID is passed through the fetch method, it is given/not given to the sync method as undefined. This is likely the reason for the issue though according to the documentation an object containing an id should be able to be passed to the fetch method. Most certainly I'm unsure as to how a singleton can be effectively used without being able to pass an object containing id through the fetch method.
  2. Sam Cleaver 2013-06-24

    After reading through some Backbone documentation it seems you can retrieve results using the .get method (as opposed to fetch). I was confused by the Sync Adapters guide in the documentation as it shows an example whereby a Model is retrieved using the fetch method:
       // Performs a GET on /library/1
       book.fetch({id:1}); 
       
    Doesn't look like a bug so request closure.

JSON Source