PDA

View Full Version : Creating a new load() method on a model



Matt_A
24 Mar 2016, 4:29 AM
Hi,

I'm quite new to Ext and JS, so may have this completely wrong, but I'm looking to have two methods to read data into a model - a standard read method which takes the ID field and a 2nd method that reads default values from a second url based on a DefaultID (that isn't stored in the model).

I'm trying to define it in the model like this:


Ext.define('App.MyModel', {
extend: 'Ext.data.Model',
...
proxy:{
type: 'ajax',
reader: {
type: 'xml',
root: 'data'
},
writer: {
type: 'xml'
},
api: {
create: '/bti/te_web_services?action=CREATE_TRANSPORT'
// read:
// update:
// destroy:
}
},
defaultProxy:{
type: 'ajax',
reader: {
idProperty: 'TASK',
type: 'xml',
root: 'data'
},
extraParams: {
TASK : ''
},
url: '/bti/te_web_services?action=GETTRANSPORTDEFAULTS'


},
loadDefaults: function(taskId) {
// use the defaultProxy and revert back to the original after
var oldProxy = this.getProxy();
var oldIdProperty = this.idProperty;
try {
// this.idProperty = 'TASK'
console.log(this);
this.setProxy(this.defaultProxy);
this.defaultProxy.
this.load(taskId);
}
finally {
this.setProxy(oldProxy);
this.idProperty = oldIdProperty;
}
}
});




This would be called like this:



var transportRecord = Ext.create('App.MyModel', {
TASK : this.getCurrentTaskId()
});
transportRecord.loadDefaults(transportRecord.data.TASK, {
success: function() {
newTransportEntryForm.getForm().loadRecord(transportRecord);
newTransportEntryForm.show();
}
});




However I'm getting the code faulting complaining that this.load is not a function (though code completion in Chrome debugger seems to find save() OK?

Can I get a pointer into what I'm doing wrong? Also, is there a neater way to have a fifth operation in addition to the normal CRUD to avoid switching to a 2nd proxy which feels a bit hacky?

Matt_A
24 Mar 2016, 8:00 AM
OK - I've realised I'm getting confused with static methods.It appears I can call my own 'CRUD' method by adding an extra string to the 'api' definition and then using that in the 'action' field of the config object passed into load().Unfortunately for me, I've just discovered that the API that I'm using doesn't map the fields from the two calls so I need a whole new model anyway :-(