如何将多个(动态)参数传递给store.find()方法//避免重复托管

时间:2015-04-23 07:44:53

标签: ember.js ember-data

我们有一个带有多个(动态生成的)输入字段的搜索公式。 我们的服务器rest api是Spring MVC驱动的,接受这样的调用:

/rest/search/1?identnummer=0000000&parameter2=xy&parameter3=zy

在公式的搜索方法中(动作:搜索)

我收集所有参数并建立一个这样的字符串:

queryparams =  1?identnummer=0000000&parameter2=xy&parameter3=zy

然后调用store.find方法:

that.store.find('trefferliste', queryparams).then(function(response) {
            console.log("Success loading hit list...", response);
        }, 
        function(error) {
            console.error(error);
                that.transitionTo('trefferliste');
             }
        });

我覆盖了buildURL方法(删除了encodeURIComponent()

if (id && !Ember.isArray(id)) { url.push(encodeURIComponent(id)); }

用于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;
}
});

在发送请求并收到预期的json响应后,我在商店中有两个托管:

Entry 1 :  id = 1
Entry 2 :  id = 1?identnummer=0000000&parameter2=xy&parameter3=zy

第一个条目是预期的条目,第二个条目是损坏的并导致此错误:

Error: Assertion Failed: Expected an object as `data` in a call to `push` for App.Trefferliste , but was undefined
at new Error (native)
at Error.EmberError (/js/libs/ember-stable/ember.debug.js:12061:21)
at Object.Ember.default.assert (/js/libs/ember-stable/ember.debug.js:5162:13)
at ember$data$lib$system$store$$Service.extend.push (/js/libs/ember-canary/ember-data.js:10849:15)
at /js/libs/ember-canary/ember-data.js:4805:24
at Object.Backburner.run (/js/libs/ember-stable/ember.debug.js:217:27)
at ember$data$lib$system$store$$Service.extend._adapterRun (/js/libs/ember-canary/ember-data.js:11143:33)
at /js/libs/ember-canary/ember-data.js:4802:22
at tryCatch (/js/libs/ember-stable/ember.debug.js:46977:16)
at invokeCallback (/js/libs/ember-stable/ember.debug.js:46989:17)

所以我的问题是:

  • 这是将动态参数传递给的可接受方法吗? 找方法?
  • 有人知道如何防止腐败物品 进店?

有关我的设置的更多信息:

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

用于加载我的记录的序列化方法:

/**
Deserialize nested JSON payload into a flat object with sideloaded relationships. */
App.NestedSerializer = DS.RESTSerializer.extend({

/**
Generate a unique ID for a record hash
@param {DS.Store} store
@param {DS.Model} type
@param {Object} hash The record hash object
@return {String} The new ID
*/
generateID: function(store, type, hash){
var id, primaryKey,
serializer = store.serializerFor(type);

type._generatedIds = type._generatedIds || 0;

// Get the 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 a record hash to the payload

@method sideloadRecord
@param {DS.Store} store
@param {DS.Model} type
@param {Object} payload The entire payload
@param {Object} hash    The record hash object
@return {Object} The ID of the record(s) sideloaded
*/
sideloadRecord: function(store, type, payload, hash) {
var id, sideLoadkey, sideloadArr, serializer;

// If hash is an array, sideload each item in the 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 are plural
  sideloadArr = payload[sideLoadkey] || []; // Get/create sideload array
  id = this.generateID(store, type, hash);

  // Sideload, if it's not already sideloaded
  if (sideloadArr.findBy('id', id) === undefined){      
    sideloadArr.push(hash);
    payload[sideLoadkey] = sideloadArr;
  }
}
// Assume it's already pointing to a sideloaded ID
else {
  id = hash;
}

return id;
},

/**
Process nested relationships on a single hash record

@method extractRelationships
@param {DS.Store} store
@param {DS.Model} type
@param {Object} payload The entire payload
@param {Object} hash    The hash for the record being processed
@return {Object} The updated hash object
*/
processRelationships: function(store, type, payload, hash) {
// If hash is an array, process each item in the array
if (hash instanceof Array) {
  hash.forEach(function(item, i){
    hash[i] = this.processRelationships(store, type, payload, item);  
  }, this);
}

else {

  // Find all relationships in this model
 type.eachRelationship(function(key, relationship) {
    var related = hash[key],         // The hash for this relationship
        relType = relationship.type; // The 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 the deserialized of all payloads.

@method normalizePayload
@param {Object} payload
@return {Object} the normalized payload
 */
extract: function(store, type, payload, id, requestType) {

payload = this.normalizePayload(payload);
var rootKeys = Ember.keys(payload);

// Loop through root properties and process extract their relationships
rootKeys.forEach(function(key){
  var type = store.container.lookupFactory('model:' + key.singularize()),
      hash = payload[key];

// If the key resolves to a model type, sideload any 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;

1 个答案:

答案 0 :(得分:-1)

您可以像这样监控查询参数:

#..controller
queryParamsDidChange: (->
  @store.find('myModel', @get('customQueryParams')).then (models)=>
    @set 'content', models
).observes 'customQueryParams'

customQueryParams将是{myParam1: '123', myParam2: '456'}

之类的对象

这对你有用吗?