0

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?

arya
  • 81
  • 1
  • 8

1 Answers1

1

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.

Webby Vanderhack
  • 889
  • 7
  • 13
  • Ok I got it now.. I have different question,how can I explicitly set Model attribute from outside. I want to do something like this. – arya Oct 28 '15 at 09:13
  • I can't answer you fully because I can't get your code (or any really) to return a proper response from that source. I can obviously see the data when I go to it in my browser. Anyway, I'm still a bit confused. You make a new AccountSetup model and then fetch it. Assuming everything worked it sounds like you want to provide a success call back to fetch: accountSetup.fetch({success: enyo.bindSafely(function(model) {...})}); At this point you have the model and can do whatever you want with/to it. – Webby Vanderhack Oct 28 '15 at 23:09
  • I can't edit my last response (too old!) but the example should be this instead: accountSetup.fetch({success: enyo.bindSafely(this, function(model) {...})}); – Webby Vanderhack Oct 29 '15 at 22:16