extjs4.2 - ExtJs 4.2 Extend Custom Model using MVC problems -
using extjs 4.2 mvc pattern
i trying make custom model, store, proxy, reader, writer having problems getting work in mvc pattern. followed this example extend model , can see working if not used in mvc way.
my store refers model such contacts defined in model property, contacts refers custom model wakandamodel using model property. when create store refers contacts none of model properties or proxy defined in custom wakandamodel brought on stores model.
here code, have left comments in can see have attempted try. help!
app code
ext.loader.setconfig({ enabled : true, paths : { 'ext.ux' : "lib/extux", 'wakanda' : "lib/extux/wakanda" } }); ext.application({ name : 'simplyfundraising', autocreateviewport : true, requires : ['ext.ux.router', // require ux 'ext.window.messagebox'], controllers : ['contacts'], }); custom model
ext.define('wakanda.model', { extend: 'ext.data.model', idproperty: '__key', stampproperty: '__stamp', defaultproxytype: 'wakanda', onclassextended: function(cls, data) { // debugger; // cls.apply(this) // var parts = data.$classname.split('.'); // var entity = parts[2] // var catalog = this.prototype.getcatalog(entity), // attributes = catalog.attributes; // (var = 0, l = attributes.length; < l; i++) { // if (attributes[i].name === 'id') { // attributes[i].persist = false; // } // } // attributes.push({name: this.prototype.idproperty}); // attributes.push({name: this.prototype.stampproperty}); // // data.fields = attributes; // // debugger; // //this.setfields(data.fields) // // var mymodel = ext.modelmanager.getmodel(data.$classname); // debugger; // ext.appy(this); // //this.superclass.superclass.$onextended.apply(this, arguments); }, getcatalog: function(classname) { var catalog; ext.ajax.request({ async: false, url: 'http://127.0.0.1:8081/cors/$catalog/' + classname, success: function(response) { catalog = ext.decode(response.responsetext); } }); return catalog; } }); custom proxy
ext.define('wakanda.proxy', { extend: 'ext.data.proxy.rest', // alternateclassname: 'simplyfundraising.data.wakandaproxy', alias : 'proxy.wakanda', sortparam: '$orderby', filterparam: '$filter', startparam: '$skip', limitparam: '$top', // groupersparam: '$group', reader: 'wakanda', writer: 'wakanda', actionmethods: { create : 'post', read : 'get', update : 'post', destroy: 'post' }, buildurl: function(request) { debugger; var modelname = this.model.modelname, operation = request.operation, records = operation.records || [], record = records[0], id = record ? record.getid() : operation.id, url = '/cors/' + modelname, action = request.action; if (this.appendid && id && (action === 'read' || action === 'destroy')) { url += '(' + id + ')'; } request.url = url; // console.log("buildurl", this, arguments, request.url); if (action !== 'read') { if (action === 'create') action = 'update'; else if (action === 'destroy') action = 'delete'; url = ext.urlappend(url, '$method=' + action); } if (this.nocache) { url = ext.urlappend(url, ext.string.format("{0}={1}", this.cachestring, ext.date.now())); } return url; }, encodesorters: function(sorters) { var min = [], length = sorters.length, = 0, sort = ''; (; < length; i++) { sort += sorters[i].property + ' ' + sorters[i].direction + ' '; } return sort; }, encodefilters: function(filters) { var min = [], length = filters.length, = 0, filter = ''; (; < length; i++) { filter += filters[i].property + ' eq ' + filters[i].value + '@ '; } return filter; } }); custom reader
ext.define('wakanda.reader', { extend: 'ext.data.reader.json', //alternateclassname: 'simplyfundraising.data.wakandareader', alias : 'reader.wakanda', root: '__entities', totalproperty: '__count', getdata: function(data) { if (ext.isobject(data) && !data[this.root]) { data = [data]; } return data; } }); custom writer
ext.define('wakanda.writer', { extend: 'ext.data.writer.json', // alternateclassname: 'simplyfundraising.data.wakandawriter', alias: 'writer.wakanda', writeallfields: false, getrecorddata: function(record) { var isphantom = record.phantom === true, writeall = this.writeallfields || isphantom, nameproperty = this.nameproperty, fields = record.fields, data = {}, changes, name, field, key; if (writeall) { // console.log("getrecorddata1", this, arguments); fields.each(function(field){ if (field.persist) { name = field[nameproperty] || field.name; data[name] = record.get(field.name); } else { } }); } else { changes = record.getchanges(); // console.log("getrecorddata2", this, arguments, changes); (key in changes) { if (changes.hasownproperty(key)) { field = fields.get(key); name = field[nameproperty] || field.name; data[name] = changes[key]; } } if (!isphantom) { data[record.idproperty] = record.getid(); data[record.stampproperty] = record.get(record.stampproperty); } } return {'__entities': [data]}; } }); contacts model
ext.define('simplyfundraising.model.contact', { extend : 'wakanda.model' , //constructor: function() { //alert(“going call parent’s overriden constructor…”); // this.callparent(arguments); // return this; // } }); contacts store
ext.define('simplyfundraising.store.contacts', { extend : 'ext.data.store', model : 'simplyfundraising.model.contact', autoload : true, autosync : true, // constructor: function() { // this.model = ext.create('simplyfundraising.model.contact') //alert(“going call parent’s overriden constructor…”); // this.callparent(arguments); return this; // } }); contacts controller
ext.define('simplyfundraising.controller.contacts', { extend : 'ext.app.controller', models : ['contact'], views : ['contacts.list', 'contacts.edit'], init : function() { this.control({ 'contactslist' : { itemdblclick : this.editcontact, removeitem : this.removecontact }, 'contactslist > toolbar > button[action=create]' : { click : this.oncreatecontact }, // 'contactsadd button[action=save]': { // click: this.docreatecontact // }, 'contactsedit button[action=save]' : { click : this.updatecontact } }); }, list : function() { // var mystore = ext.storemgr.lookup('contacts'); // var mycontact = this.getmodel('contact') // var user = this.getmodel('user'); //debugger; // var mystore = ext.create('simplyfundraising.store.contacts') // var mycontact = this.getmodel('contact').create() // var bb = mycontact.create() // var rr = ext.create('simplyfundraising.model.contact') var mystore = ext.create('simplyfundraising.store.contacts') debugger; // mystore.proxy.api.read = users.proxy.api.read + '(17)' //mystore.proxy.extraparams = { $expand: 'contacttype'}; mystore.load(); //var test = ext.modelmanager.getmodel('contact'); // //var user = this.getcontactmodel(); // user.load(258, { // success: function(user) { // console.log("loaded user 258: " + user.get('lastname')); // } // }); }, editcontact : function(grid, record) { var view = ext.widget('contactsedit'); view.down('form').loadrecord(record); this.addnew = false }, removecontact : function(contact) { ext.msg.confirm('remove contact ' + contact.data.lastname, 'are sure?', function(button) { if (button == 'yes') { this.getcontactsstore().remove(contact); } }, this); }, oncreatecontact : function() { var view = ext.widget('contactsedit'); this.addnew = true }, // docreatecontact: function (button) { // var win = button.up('window'), // form = win.down('form'), // values = form.getvalues(), // store = this.getcontactsstore(); // if (form.getform().isvalid()) { // store.add(values); // win.close(); // } // }, updatecontact : function(button) { var win = button.up('window'), form = win.down('form'), record = form.getrecord(), values = form.getvalues(), store = this.getcontactsstore(); if (form.getform().isvalid()) { if (this.addnew == true) { store.add(values); } else { record.set(values); } win.close(); } } });
i got working now:
it looks custom wakanda model,proxy,reader,writer not loading. still bit confused on how mvc references classes , loads files , instantiates classes, asked in other question.
any way fix add requires custom wakanda model , proxy
for wakanda model add requires: ['wakanda.proxy'],
wakanda proxy add requires: ['wakanda.reader', 'wakanda.writer'],
now inheritance working expected.
Comments
Post a Comment