Skip to content

@cmilfont /custom_model.js
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Custom Model for ExtJS
Ext.define("CustomRest", {
extend: 'Ext.data.proxy.Rest',
alias : 'proxy.customrest',
setAction: function(config) {
this[config.name] = function(model, options){
Ext.apply(options, {
records:[model],
record: model,
action : config.name
});
var scope = options.scope || model;
var operation = new Ext.data.Operation(options);
var callback = function(operation) {
args = [model, operation];
if (operation.wasSuccessful()) {
Ext.callback(options.success, scope, args);
} else {
Ext.callback(options.failure, scope, args);
}
Ext.callback(options.callback, scope, args);
};
return this.doRequest.apply(this, [operation, callback]);
};
this.actionMethods[config.name] = config.method || "POST";
},
getAction: function(name) {
return this.actions.filter(function(action) {
return action.name === name;
})[0];
},
pattern: /:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g,
interpolate: function(url, model) {
return url.replace(this.pattern, function(_, property) {
return model.get(property);
});
},
buildUrl: function(request) {
var url = this.callParent(arguments);
var action = this.getAction(request.action);
if(action && action.url) {
url = this.interpolate(action.url, request.operation.record) + ".json";
}
return url;
},
constructor: function(config) {
this.actions = config.actions;
this.callParent([config]);
if(config && config.actions) {
for(action in config.actions) {
this.setAction( config.actions[action] );
}
}
}
});
Ext.define('CustomModel', {
extend:'Ext.data.Model',
merge: function(fields) {
for(var field in fields) {
this.set(field, fields[field]);
}
},
constructor: function() {
this.callParent(arguments);
this.setProxy({
type: 'customrest',
url: this.url,
format: 'json',
actions: this.actions
});
var self = this;
if(self.actions) {
for(position in self.actions) {
var action = self.actions[position];
self[action.name] = (function(act) {
return function(options) {
self.doAction(act, options);
};
})(action.name);
}
}
},
doAction: function(action, options) {
this.getProxy()[action](this, options);
}
});
Ext.override(CustomModel, {
statics: {
load: function() {
this.setProxy({
type: 'rest',
url: this.prototype.url,
format: 'json'
});
this.callParent(arguments);
}
}
});
Ext.define('Sistema', {
extend: 'CustomModel',
fields: ['id','nome','cliente_id'],
actions: [
{
name: "por_cliente",
method: "GET",
url: "/clientes/:cliente_id/sistemas/por_cliente"
}
],
url: "/sistemas"
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.