Unable to throw event enyo.model

111 Views Asked by At

I have following code.I am trying to send event to other kind when I get response of ajax query.I am using enyo JsonpSource.

new enyo.JsonpSource({ name: "json" });
//new enyo.AjaxSource({ name: "json" });

enyo.kind({

name: "AccountSetup",


//kind: "enyo.Model",

source: "json", 

options: { parse: true },

getUrl: function () {

    return "https://223.30.248.51:1926/HotelInfoServlet?Q=ACTIVATE&UA=1300029800&MODE=JSON";

},

events: 
{     onMyEvent: ''
},

gotData : function(){

    //this.inherited(arguments);
    console.log("In gotData \n");
         //   console.log(tmp);
    console.log(accountSetup);
   this.doMyEvent({answer:accountSetup});

},
 //this.doMyEvent({answer:accountSetup});
success : function(){
        alert("Here!\n");
    },

     });



var accountSetup = new AccountSetup();

accountSetup.fetch();

/* process callback data*/

function callback  (data)
{
if (data.type == "accountsetup") {
    console.log(data);
    console.log(this);  
    var acsetup = new AccountSetup();
       accountSetup= data;
       //acsetup = data;
        console.log(acsetup);
        //acsetup.doMyEvent({answer : data});
        //accountSetup.gotData();
        acsetup.gotData();
}

};

In this,I get error as Uncaught TypeError: accountSetup.fetch is not a function.

If I removed comment of enyo.model, I get error as Uncaught TypeError: this.doMyEvent is not a function.

Where is the problem occurring?

1

There are 1 best solutions below

3
Webby Vanderhack On

Okay, a couple of things.

  1. It has to be a Model or ModelController (maybe Collection) to have the fetch() method.
  2. Models are not based on enyo.Object, so they behave a bit differently than other Enyo objects, most importantly, in this case, with respect to how the Enyo event system works. You can emit an event from the model when you parse its data, I suppose.
  3. I'm confused about how/when gotData() will run. If you want to do something when parsing the data (I assume you do, since you have parse:true option set) then you should provide the parse() method as an override. In it, you would get the raw data and return what you want the model to actually have.

I think you may need to take a look more closely at how enyo.Model works.