Can I bubble-up an event from Ajax.response?

I've been struggling with this for a day. I can't figure out how to bubble an event up from the ajax.response() (or ajax.error() handlers coming from the fetch() of my enyo/Source...

I could really use some help on understanding how to bubble-up an event to be handled by a parent kind - I am currently calling mftLbUserLoginModel.fetch(); from a parent's Button ontap event, and the Ajax is working fine, I just can't figure out how to handle UI changes based on success or failure. Here is the enyo/Source and enyo/Model I'm working with:

var kind = require('enyo/kind'),
  EnyoSource = require('enyo/Source'),
  Ajax = require('enyo/Ajax'),
  Model = require('enyo/Model');

// MUST CHANGE FOR PRODUCTION: lbApiUrl
  var mftLbApiUrl = 'http://192.168.0.136:3000/api/';
  var mftLbLoginEndpoint = 'mftUsers/login';
  var mftGetUserWithIdEndpoint = 'mftUsers/';

var MftLbLoginSource = kind({
  name: 'mftLbLoginSource',
  kind: EnyoSource,
  fetch: function (rec, opts) {
    // implement code to fetch records
    var ajax = new Ajax({
      url: mftLbApiUrl + mftLbLoginEndpoint,
      method: 'POST',
      cacheBust: false,
      postBody: {username: rec.get('username'), password: rec.get('password')}
    });
    
    ajax.go();

    ajax.response(this, function (inSender, inResponse) {
      opts.success(inResponse); // call success callback to return data
      // ** How to bubble a loginSuccess event from here **
      // get username from API with get /mftUsers/{id}
      mftLbUserModel.fetch();
    });
    
    ajax.error(this, function (inError, err) {// WIP: Needs Work...
      opts.error(inError);
      mftLbUserLoginModel.clearError();
      mftLbUserModel.set('username', undefined); // TEMP - Looks ugly, shoudln't this send an event to be handled
    });
  }
});
var mftLbLoginSource = new MftLbLoginSource({name: 'mftLbLoginSource'});

var MftLbUserLoginModel = kind({
  name: "mftUserLoginModel",
  kind: Model,
  source: 'mftLbLoginSource',
  url: mftLbApiUrl + mftLbLoginEndpoint,
  options: {parse: true},
  parse: function (data) {
    data.access_token = data.id; // Loopback access_token is returned as 'id'
    return data;
  }
});

var mftLbUserLoginModel = new MftLbUserLoginModel({name: "mftLbUserLoginModel"});
-Thanks in advance for any help!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!