Mootools calling class method onSuccess Ajax -
i'm trying achieve following:
- user clicks on element , cmove function invoked (works)
- cmove passes js object ajax method (works)
- ajax method submits object move/test controller (php/codeigniter) (works)
- controller returns json data (works)
- onsucces calls move method stuff (no chance calling method)
so, how can call move method out of onsuccess? , there better way of pushing moveobj around?
var plane = new class({ implements: options, options: { action: null }, initialize: function(options) { this.setoptions(options); $('somelement').addevent('click', this.cmove.bind(this)); }, cmove: function(event, action) { moveobj = new object(); moveobj.x = 123; this.ajax('cmove', moveobj); }, move: function(moveobj) { console.log("yippe!"); // not getting here :( }, ajax: function(action, obj) { resp = $('resp'); var myrequest = new request.json({ url: '<?php echo site_url('move/test'); ?>', method: 'get', onrequest: function(){ resp.set('text', 'wait...'); }, onsuccess: function(responsetext){ switch(action) { case 'cmove': test3.move(responsetext); // not working this.move(responsetext); // not working parent.move(responsetext); // not working resp.set('text', responsetext.x); break; }, onfailure: function(){ resp.set('text', 'sorry, request failed :('); } }).send('coords='+ json.encode(obj)); } }); window.addevent('domready', function(){ var test3= new plane({}); });
you need bind callback function request instance scope of class or save reference of class' instance (self = this) , call via instead:
ajax: function(action, obj) { var self = this; // save refrence var resp = document.id("resp"); new request.json({ url: '<?php echo site_url('move/test'); ?>', method: 'get', onrequest: function(){ resp.set('text', 'wait...'); }, onsuccess: function(responsetext){ switch(action) { case 'cmove': self.move(responsetext); // work this.move(responsetext); // work break; } }.bind(this), // binding callback instance this.method works onfailure: function(){ resp.set('text', 'sorry, request failed :('); } }).send('coords='+ json.encode(obj)); }
Comments
Post a Comment