enyo.Collection callback after didFetch()

I am trying to run a function after a successful fetch() of an enyo.Collection.
I have tried using the didFetch() with gives access to
didFetch: function(rec, opts, res)
those parameters, but nothing to bubble up as per its description in the API.

I am wondering how to use the following piece of information to add listeners to then fire upon success.
Collection objects generate add, remove, reset and destroy events that may be listened for using the addListener() method.
Does anyone have an example of using the addListener() method regarding a successful enyo.Collection fetch() ?

Comments

  • For 2.5 we're actually revising the status of an enyo.Collection to be stateful, so stay tuned for that. The current documentation is being updated to reflect this. Are you using the tagged 2.4 release or working off master?
  • edited July 2014
    Edit: Please see chrisvanhooser's answer below for the preferred method, forgot about the opts. :)
  • edited July 2014
    Thanks for the replies, but i've tried using the success handler before. I may be slightly lost but I've not been able to get outside the scope of that success block in order to call a different function...
    I'm used to calling functions with this.functionName() or something, however logging this in the success block I get the following object,
    Object {success: function, fail: function, strategy: "add"}
    fail: function (fail){
    strategy: "add"
    success: function (data)
    So how would one call a function from here in the success block?

    As well I would still love an example of using the addListener() method.
    Also what could i do with enyo.bind here?

    Thanks for any assistance!

    cheers
  • edited July 2014
    You can wrap your success function with enyo.bindSafely, i.e.
    
    this.collection.fetch({success: enyo.bindSafely(this, 'successFunction'), fail: enyo.bindSafely(this, 'failFunction'), strategy: 'add'}
    failFunction: function(data) {
        // do something upon failure
    },
    successFunction: function (data) {
        // do something upon success
    }
    
    To use the addListener() method, you can call it directly on the enyo.Collection kind, i.e.
    
    this.collection.addListener('add', function(sender, name) {
        // do something when 'add' is fired
    });
    
  • Thank you very much!
    That bindSafely() solves several of my problems, I wish I had come across that earlier!

    cheers
Sign In or Register to comment.