ember.js - How to pass multiple (dynamic) Parameter to store.find() method // avoid duplicate entrys -
we have search formular multiple (dynamic generated) input fields. our server rest api spring mvc powered , accepts call this:
/rest/search/1?identnummer=0000000¶meter2=xy¶meter3=zy
in search method of formular (action: search)
i collect parameters , build string this:
queryparams = 1?identnummer=0000000¶meter2=xy¶meter3=zy
and call store.find method:
that.store.find('trefferliste', queryparams).then(function(response) { console.log("success loading hit list...", response); }, function(error) { console.error(error); that.transitionto('trefferliste'); } });
i overwrote buildurl method (removed encodeuricomponent()
if (id && !ember.isarray(id)) { url.push(encodeuricomponent(id)); }
for trefferlisteadapter
app.trefferlisteadapter = app.applicationadapter.extend({ buildurl: function(type, id) { var url = []; var host = helicdevserver; var prefix = this.urlprefix(); if (type) { url.push(this.pathfortype(type)); } if (id && !ember.isarray(id)) { url.push(id); } if (prefix) { url.unshift(prefix); } url = url.join('/'); if (!host && url) { url = '/' + url; } return url; } });
after request send , receive expected json response, have 2 entrys in store:
entry 1 : id = 1 entry 2 : id = 1?identnummer=0000000¶meter2=xy¶meter3=zy
the first entry expected one, second corrupt , causes error:
error: assertion failed: expected object `data` in call `push` app.trefferliste , undefined @ new error (native) @ error.embererror (/js/libs/ember-stable/ember.debug.js:12061:21) @ object.ember.default.assert (/js/libs/ember-stable/ember.debug.js:5162:13) @ ember$data$lib$system$store$$service.extend.push (/js/libs/ember-canary/ember-data.js:10849:15) @ /js/libs/ember-canary/ember-data.js:4805:24 @ object.backburner.run (/js/libs/ember-stable/ember.debug.js:217:27) @ ember$data$lib$system$store$$service.extend._adapterrun (/js/libs/ember-canary/ember-data.js:11143:33) @ /js/libs/ember-canary/ember-data.js:4802:22 @ trycatch (/js/libs/ember-stable/ember.debug.js:46977:16) @ invokecallback (/js/libs/ember-stable/ember.debug.js:46989:17)
so questions are:
- is acceptable method pass dynamic parameters find method?
- does know how can prevent corrupt item in store?
some more information setup:
debug: ember : 1.11.3 ember.debug.js:5197 debug: ember data : 1.0.0-beta.17+canary.4ad70aab2a ember.debug.js:5197 debug: jquery : 2.1.3
serializer method sideloading records:
/** deserialize nested json payload flat object sideloaded relationships. */ app.nestedserializer = ds.restserializer.extend({ /** generate unique id record hash @param {ds.store} store @param {ds.model} type @param {object} hash record hash object @return {string} new id */ generateid: function(store, type, hash){ var id, primarykey, serializer = store.serializerfor(type); type._generatedids = type._generatedids || 0; // id property name primarykey = ember.get(serializer, 'primarykey') || 'id'; id = hash[primarykey]; // generate id if (!id) { id = ++type._generatedids; hash[primarykey] = id; } return id; }, /** sideload record hash payload @method sideloadrecord @param {ds.store} store @param {ds.model} type @param {object} payload entire payload @param {object} hash record hash object @return {object} id of record(s) sideloaded */ sideloadrecord: function(store, type, payload, hash) { var id, sideloadkey, sideloadarr, serializer; // if hash array, sideload each item in array if (hash instanceof array) { id = []; hash.foreach(function(item, i){ id[i] = this.sideloadrecord(store, type, payload, item); }, this); } // sideload record else if (typeof hash === 'object') { sideloadkey = type.typekey.pluralize(); // sideloaded keys plural sideloadarr = payload[sideloadkey] || []; // get/create sideload array id = this.generateid(store, type, hash); // sideload, if it's not sideloaded if (sideloadarr.findby('id', id) === undefined){ sideloadarr.push(hash); payload[sideloadkey] = sideloadarr; } } // assume it's pointing sideloaded id else { id = hash; } return id; }, /** process nested relationships on single hash record @method extractrelationships @param {ds.store} store @param {ds.model} type @param {object} payload entire payload @param {object} hash hash record being processed @return {object} updated hash object */ processrelationships: function(store, type, payload, hash) { // if hash array, process each item in array if (hash instanceof array) { hash.foreach(function(item, i){ hash[i] = this.processrelationships(store, type, payload, item); }, this); } else { // find relationships in model type.eachrelationship(function(key, relationship) { var related = hash[key], // hash relationship reltype = relationship.type; // model type this.generateid(store, type, hash); hash[key] = this.sideloadrecord(store, reltype, payload, related); this.processrelationships(store, reltype, payload, related); }, this); } return hash; }, /** (overloaded method) starts deserialized of payloads. @method normalizepayload @param {object} payload @return {object} normalized payload */ extract: function(store, type, payload, id, requesttype) { payload = this.normalizepayload(payload); var rootkeys = ember.keys(payload); // loop through root properties , process extract relationships rootkeys.foreach(function(key){ var type = store.container.lookupfactory('model:' + key.singularize()), hash = payload[key]; // if key resolves model type, sideload embedded relationships if (type) { this.processrelationships(store, type, payload, hash); } }, this); console.log(payload); return this._super(store, type, payload, id, requesttype); } , normalizepayload: function(payload) { if(payload.elementbeanwrapper){ payload.dashboard = payload.elementbeanwrapper; delete payload.elementbeanwrapper; } return payload; } }); app.applicationserializer = app.nestedserializer;
you can monitor query params this:
#..controller queryparamsdidchange: (-> @store.find('mymodel', @get('customqueryparams')).then (models)=> @set 'content', models ).observes 'customqueryparams'
customqueryparams
object {myparam1: '123', myparam2: '456'}
will work you?