我有以下JSON(简单示例):
{
id: 101,
firstName: "John",
surname: "Doe"
}
Run Code Online (Sandbox Code Playgroud)
但我希望我的模型可以lastName代替使用surname.这样的事情,也许:
App.Person = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string', { key: 'surname' })
});
Run Code Online (Sandbox Code Playgroud)
我可以发誓我在某个地方看到了如何做到这一点,但对于我的生活,找不到它.我也没有在ember-data源中找到任何明显的东西.
我试过设置key,name,id,alias,并map在属性选项,但没有人可以做的伎俩.有没有办法做到这一点?
javascript model-view-controller javascript-framework ember.js ember-data
我正在建立一个电子商务网站,并将购物车作为模型,可通过网络服务/ api/cart /访问.使用RESTAdapter,我想简单地调用App.Cart.find()并始终返回1个对象,而不是数组.购物车没有ID,因此调用App.Cart.find(1)是错误的 - 加上会导致错误的Web服务调用:/ api/cart/1.
我是否需要扩展RESTAdapter或Model以使app.Cart上的find()始终返回一个对象而不是一个列表?
我有3个emberData模型:
App.Product = DS.Model.extend({
page_title: DS.attr('string'),
shop: DS.belongsTo('App.Shop'),
user: DS.belongsTo('App.User')
});
App.Shop = DS.Model.extend({
name: DS.attr('string'),
});
App.User = DS.Model.extend({
name: DS.attr('string')
});
Run Code Online (Sandbox Code Playgroud)
并且JSON数据如下所示:
{
products: [
{
id: "1",
page_title: "Product 1",
user_id: "1",
shop_id: "1",
},
{
id: "2",
page_title: "Product 2",
user_id: "2",
shop_id: "1",
}
],
users: [
{
id: "1",
name: "User 1"
},
{
id: "2",
name: "User 2"
}
],
shops: [
{
id: "1",
name: "Shop 1"
}
]
}
Run Code Online (Sandbox Code Playgroud)
但是当我加载数据时,我收到以下错误:
Assertion …Run Code Online (Sandbox Code Playgroud) 夹具包含联系人列表,每个联系人都有联系类型.我试图使用.findQuery()过滤联系人记录,但它抛出以下错误:
Uncaught TypeError: Object function () {.....} has no method 'findQuery'
Run Code Online (Sandbox Code Playgroud)
我在这里列出了我的代码:
Grid.Store = DS.Store.extend({
revision: 12,
adapter: 'DS.FixtureAdapter'
});
Grid.ModalModel = DS.Model.extend({
fname: DS.attr('string'),
lname: DS.attr('string'),
email: DS.attr('string'),
contactno: DS.attr('string'),
gendertype: DS.attr('boolean'),
contactype: DS.attr('number')
});
Grid.ModalModel.FIXTURES = [
{
id: 1,
fname: "sachin",
lname: "gh",
email: "gh",
contactno: "4542154",
gendertype: true,
contactype: 1
},
{
id: 2,
fname: "amit",
lname: "gh",
email: "gh",
contactno: "4542154",
gendertype: true,
contactype: 2
},
{
id: 3,
fname: "namit",
lname: "gh",
email: "gh", …Run Code Online (Sandbox Code Playgroud) 我有烬模型叫survey,question和response.surveys有多个questions,有多个responses.每个response都有一个属性count.
如何total_response_count在survey模型中设置计算值?在emberjs 1.0.0中,questions它们位于DS.PromiseArray中(由于async:true),因此当我返回计算值时,它在我的模板中显示为Object而不是值.
我可以轻松地responses从question模型中访问,因为responses它嵌入了question.但是,Ember会自动为questions引用的承诺生成survey因为{async:true}.
调查模型:
App.Survey = DS.Model.extend({
title: DS.attr('string'),
owner_id: DS.belongsTo('user'),
questions: DS.hasMany('question', {async:true}),
total_responses: function() {
var question_cb = function(prevValue, item) {
return prevValue + item.get('total_responses');
};
return this.get('questions').then(function(questions){
return questions.reduce(question_cb, 0);
});
}.property('questions')
});
Run Code Online (Sandbox Code Playgroud)
问题模型:
App.Question = DS.Model.extend({
survey: DS.belongsTo('survey'),
question: DS.attr('string'),
responses: …Run Code Online (Sandbox Code Playgroud) 如何使用Ember RESTAdapter使用POST动词更新或删除记录?默认情况下,它使用PUT或DELETE谓词发送json.在我工作的地方阻止使用这些动词发送.
我有点希望我可以在发送POST的情况下执行Rails,并告诉它是否使用额外的元信息秘密地使用PUT或DELETE.
我正在通过RESTAdapter使用Ember 1.0.0和ember-data 1.0.0beta2.
我想在我的Notes.Application中构建一个简单的搜索框来搜索我的笔记.我正在寻找这个网站,Ember表格和谷歌,并没有那么多的解决方案.我发现只有两个,他们不适合我的应用程序皱眉.问题是我不知道如何做到这一点.
这是我的应用程序:
<script type="text/x-handlebars" data-template-name="index">
<div class="wrap">
<div class="bar">
{{input type="text" class="search" placeholder="Where is my bookmark??" value=search action="query"}}
<div class="bar-buttons">
<button {{action "addNote"}}> NEW </button>
<button> HOME </button>
</div>
</div>
<aside>
<h4 class="all-notes">All Notes {{length}}</h4>
{{#each item in model}}
<li>
{{#link-to 'note' item}} {{item.title}} {{/link-to}}
</li>
{{/each}}
</aside>
{{outlet}}
</div>
</script>
Run Code Online (Sandbox Code Playgroud)
控制器:
Notes.IndexController = Ember.ArrayController.extend ({
search: '',
actions:{
query: function() {
// the current value of the text field
var query = this.get('search');
},
addNote: function () {
this.transitionToRoute('main'); …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个单元测试来测试我的控制器.我有一个计算属性,在模型上使用计算属性.
我不确定如何设置测试以将数据加载到模型中.
我有我的模特:
App.User = DS.Model.extend({
name: DS.attr('string'),
roles: DS.hasMany('role'),
isInstructor: function(){
return this.hasRole('instructor');
}.property('roles'),
hasRole: function(role_name){
var roles = this.get('roles');
if(!Ember.isEmpty(roles)){
return roles.any(function(role){
return role.get('name') === role_name;
});
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
在这里,我有我的控制器:
App.MyClassDetailsController = Ember.ObjectController.extend({
students: function () {
return this.get('users').filter(function (user) {
return !user.get('isInstructor');
});
}.property('content.users.@each')
});
Run Code Online (Sandbox Code Playgroud)
在我的测试中,当我为控制器设置内容时,我这样做:
myClassDetailsController.set('model', Ember.ObjectProxy.create({
id: 389,
name: 'bfcoding 101',
users: Ember.ArrayProxy.create({
content: [
Ember.ObjectProxy.create({id: 1, name: 'Joe', roles: Ember.ArrayProxy.create({content: [Ember.ObjectProxy.create({name: 'instructor'})]})}),
Ember.ObjectProxy.create({id: 2, name: 'vs', roles: Ember.ArrayProxy.create({content: [Ember.ObjectProxy.create({name: 'student'})]})}),
Ember.ObjectProxy.create({id: …Run Code Online (Sandbox Code Playgroud) 我是Ember的新手,我正在创建一个具有一些模型关系的App.
这些是我的模特
// models/user.js
export default DS.Model.extend({
name: DS.attr('string'),
role: DS.belongsTo('role')
});
// models/role.js
export default DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string')
});
Run Code Online (Sandbox Code Playgroud)
我的新用户路线是
import Ember from 'ember';
export default Ember.Route.extend({
model: function(){
return Ember.RSVP.hash({
newUser: this.store.createRecord('user'),
roles: this.store.findAll('role'),
});
}
});
Run Code Online (Sandbox Code Playgroud)
想法是获取角色并在表单中的选择视图中进行选择
这是我创建新用户的模板
<form {{ action "save" on="submit" }}>
{{ input value=newApplicant.name class="form-control" }}
<select name="role" class="form-control" value="role.value">
{{#each model.roles as |role|}}
<option value="{{role.id}}">{{role.name}}</option>
{{/each}}
</select>
Run Code Online (Sandbox Code Playgroud)
在此步骤之前一切正常,我选择了正确的数据,但我想保存,我不知道如何从表单中获取角色ID并插入用户
这是我的新控制器
import Ember from 'ember';
export default Ember.Controller.extend({
newUser: Ember.computed.alias('model.newUser'),
roles: null,
// …Run Code Online (Sandbox Code Playgroud) 每当他试图删除项目时,我都要求在用户名中包含用户的备注.到目前为止,我有这个:
let remarks = this.get('remarks');
let id = this.get('itemID');
this.store.findRecord('item', id).then(function (selectedItem) {
// TODO - DELETE doesn't accept payload in body?
selectedItem.destroyRecord({remarks:remarks}).then(function(response){
Ember.debug('delete successful:'+JSON.stringify(response));
Ember.$('#confirmDelete').modal('hide');
Ember.$('#remarks').val('');
context.set('successful', true);
context.set('message', context.get('i18n').t('success.role.delete'));
}).catch(function(error){
Ember.debug('delete failed:'+JSON.stringify(error));
Ember.$('#confirmDelete').modal('hide');
Ember.$('#remarks').val('');
context.send('showErrors', error);
});
});
Run Code Online (Sandbox Code Playgroud)
它不起作用.在模型中设置备注值也是如此:
...
this.store.findRecord('item', id).then(function (selectedItem) {
selectedItem.set('remarks', remarks);
selectedItem.destroyRecord().then(function(response){
...
Run Code Online (Sandbox Code Playgroud)
我试图覆盖deleteRecord但我不知道从哪里开始或如何做到这一点.
有人有想法吗?谢谢!