所以我有这个要求,当我转到whitelist
路由时,我从API获取列入白名单的用户列表。我的模型和序列化器是这样的:
模型/ whitelist.js
import BaseUser from './base-user';
var Whitelist = BaseUser.extend({});
export default Whitelist;
模型/基层 - user.js的
import Ember from 'ember';
import DS from 'ember-data';
var attr = DS.attr;
export default DS.Model.extend({
name: attr(),
username: attr(),
description: attr(),
profilePicture: attr(),
followersCount: attr(),
followsCount: attr(),
postCount: attr(),
verified: attr(),
following: attr('boolean'),
whitelisted: attr('boolean'),
blacklisted: attr('boolean'),
unfollow: followUnfollow,
follow: followUnfollow,
whitelist: whitelist,
unwhitelist: unWhitelist,
blacklist: blacklist,
unblacklist: unBlacklist,
reply: reply,
getLists: getLists,
createdAt: attr()
});
串行器/ base.js
import DS from 'ember-data';
var serializeCollection = function(store, type, payload, id, requestType) {
payload.records = payload.records ? payload.records : [];
var users = payload.records.map(function(record) {
record = record.user ? record.user : record;
var user = {
id: record.id,
name: record.name || record.fullName,
username: record.screenName || record.username,
description: record.description || record.bio,
profilePicture: record.profileImageURLHttps || record.profilePicture,
followersCount: record.followersCount,
followsCount: record.friendsCount || record.followsCount,
postCount: record.statusesCount || 0,
verified: record.verified || false,
createdAt: record.createdAt || false
};
return user;
});
return users;
};
export default DS.RESTSerializer.extend({
extractFindAll: serializeCollection,
extractFindQuery: serializeCollection
});
串行器/ whitelist.js
import Ember from 'ember';
import Base from './base';
var serializeCollection = function(store, type, payload, id, requestType) {
/* Here payload is of the form:
* {
* cursor: null,
* records: [{
* user: {}
* friendship: {}
* }]
* }
*/
var data = this._super(store, type, payload, id, requestType);
return data.map(function(item) {
return Ember.$.extend(item, {
following: true,
whitelisted: true,
blacklisted: false
});
});
};
export default Base.extend({
extractFindAll: serializeCollection,
extractFindQuery: serializeCollection
});
现在基于某些动作,我在控制器中获取一条新记录,如下所示:
actions: {
addToWhitelist: function() {
var _this = this;
Ember.$.get(url).then(function(data) {
/*
*The data here is in this form
* {
* frienship: {},
* user: {}
* }
*/
//What should I do here so that my serializers get called?
});
return false;
}
}
我该怎么办才能将记录推送到已经填充的商店,以便首先调用序列化程序,whitelist
序列化程序,然后从application
序列化程序中调用?< / p>
答案 0 :(得分:1)
虽然ED和我都建议您使用REST API。但有时您可能无法控制您的API。
Ember.Store.pushPayload正是您要找的。此方法接受格式正确的原始JSON数据并将其推送到本地商店模型。
您的工作样本可能看起来像
actions: {
addToWhitelist: function() {
var self = this;
Ember.$.get(url).then(function(data) {
self.store.pushPayload(data);
});
return false;
}
}
答案 1 :(得分:0)
您不应该使用Ember.$.get
从ember-data
中受益。您可能希望以下列方式创建记录:
actions: {
addToWhitelist: function() {
var record = this.store.createRecord('whitelist', { /* your record */ });
record.save().then(function() {
// here's after the POST / PUT requests were done
});
}
}
这有帮助吗?