Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch problem #79

Open
boardmain opened this issue Aug 8, 2014 · 2 comments
Open

Fetch problem #79

boardmain opened this issue Aug 8, 2014 · 2 comments
Milestone

Comments

@boardmain
Copy link

hi im trying sample code
i have a problem with the fetch

this is the code


var currentView = 'by_author';
var mbooks = Alloy.createCollection("Book");

mbooks.on('fetch', function(e) {
    Ti.API.info('BLOCK1');
    Ti.API.info(JSON.stringify(mbooks)); 
      mbooks.each(function(b){
         Ti.API.info(JSON.stringify(b)); 
      });
});

mbooks.fetch({ view: currentView , success:function(e){
    Ti.API.info('BLOCK2');
    Ti.API.info(JSON.stringify(mbooks)); 
}});

$.main.open();

OUTPUT

[INFO] :   BLOCK1
[INFO] :   []
[INFO] :   BLOCK2
[INFO] :   [{"author":"Asdasdasd","published":[2014,2,8],"title":"Asdasdasd","_id":"Adasdasdasd","_rev":"1-741ec948ef454814363f77945a67723a"},{"author":"Sam","title":"Ciao Ciao Ciao","_id":"52330280a12f19849db471dd750001c1","_rev":"4-bc79829c9682a1757f0cc1d3b77f9a18","pulished":[2014,20,10]}]

in the BLOCK2 mbooks contain al models, in BLOCK1 it is emply

@pegli
Copy link
Collaborator

pegli commented Aug 8, 2014

The TiTouchDB sync adapter emits the fetch event before the collection is updated. I put that event trigger in the adapter in the same place as a it occurs in the sql adapter so both would function in the same way.

I did a bit of searching and re-read the Backbone collection documentation, and it appears that fetch isn't a standard Backbone event. To get notified when an Alloy collection changes, we should be listening for the reset event, which is fired when the entire collection's contents are replaced. Try changing your code to:

mbooks.on('reset', function(e) {
  Ti.API.info('BLOCK1');
  Ti.API.info(JSON.stringify(mbooks)); 
    mbooks.each(function(b){
       Ti.API.info(JSON.stringify(b)); 
    });
  });

and you should see that the collection is populated.

@pegli pegli closed this as completed Aug 8, 2014
@boardmain
Copy link
Author

hi i studied the sql adapter and it is different
i commented out all

  !opts.silent && collection.trigger('fetch', { fromAdapter: true });

in the read, update and deleted case
and changed the

if (resp) {
    _.isFunction(opts.success) && opts.success(resp);
  } else {
    _.isFunction(opts.error) && opts.error(resp);
  }

with

  if (resp) {
        _.isFunction(opts.success) && opts.success(resp);
        "read" !== method || opts.silent || model.trigger("fetch", {
            fromAdapter: true
        });
    } else _.isFunction(opts.error) && opts.error(resp);

from the sql adapter
and now the fetch work like the others adapter ( the fetch event need to be fired after the collection changed)

@pegli pegli reopened this Aug 26, 2014
@pegli pegli added this to the 1.3 milestone Oct 28, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants