我正在尝试在Ember.js中保存包含日期属性的对象.
在商店上调用createRecord()之后,新对象在页面上显示,但是在调用save()时,日期属性从页面中消失.模型上的所有其他属性类型都不会显示此出现然后消失的行为.这是一些示例代码:
App = Ember.Application.create()
App.IndexRoute = Ember.Route.extend
model: () -> @store.find "post"
App.Post = DS.Model.extend
time: DS.attr "date"
body: DS.attr "string"
App.IndexController = Ember.ArrayController.extend
actions:
createPost: () ->
body = @get 'newBody'
post = @store.createRecord 'post',
body: body
time: Date.now()
@set 'newBody', ''
post.save()
App.Post.FIXTURES = [
id: 1
time: new Date("2013-9-30")
body: "fixture post"
]
Run Code Online (Sandbox Code Playgroud)
请看这个jsfiddle进行演示.
query-params-new标志设置为true.
使用最新的ember和ember数据.Ember:1.6.0-beta.1 + canary.b5b90241 ember.js和Ember数据:1.0.0-beta.7 + canary.b45e23ba
应用程序适配器设置为:
export default DS.RESTAdapter.extend({
host: 'http://example.com',
namespace: 'api/v2',
headers: {
"ClientId": "a-key",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "X-Requested-With"
}
});
Run Code Online (Sandbox Code Playgroud)
位置路线如下所示:
export default Ember.Route.extend({
model: function(params){
return this.store.find('restaurant');
}
});
Run Code Online (Sandbox Code Playgroud)
位置控制器看起来像这样
export default Ember.ArrayController.extend({
queryParams: ['lat', 'lon', 'limit'],
lat: null,
lon: null,
limit: null,
});
Run Code Online (Sandbox Code Playgroud)
导航到此URL http://example.com/locations?lat=47.620508&limit=22&lon=-122.349174Ember数据http://example.com/locations作为请求发送.
我肯定错过了什么?
使用ember-cli生成项目MODEL_FACTORY_INJECTIONS时,默认情况下您将启用该项目.
但由于某种原因,它会破坏夹具装载:
适配器/应用
export default DS.FixtureAdapter.extend({});
Run Code Online (Sandbox Code Playgroud)
车型/笔记
var Note = DS.Model.extend({
text: DS.attr('string'),
});
Note.FIXTURES = [
{
id: 1,
text: 'text1'
},
];
export default Note;
Run Code Online (Sandbox Code Playgroud)
路线/指数
export default Ember.Route.extend({
model: function() {
return this.store.find('note');
}
});
Run Code Online (Sandbox Code Playgroud)
随着MODEL_FACTORY_INJECTIONS我得到
加载路径时出错:错误:断言失败:无法找到模型类型注释的固定装置@ model:注意:
没有一切按预期工作.
也许我错过了什么?或者这只是一个错误?
我正在使用Ember-cli和qunit进行测试.
项目模型
import DS from 'ember-data';
var attr = DS.attr,
belongsTo = DS.belongsTo;
export default DS.Model.extend({
offer: belongsTo('offer'),
});
Run Code Online (Sandbox Code Playgroud)
这里添加测试项目和Offer模型之间的关系.
物品测试
import Ember from "ember";
import DS from "ember-data";
import { test, moduleForModel } from 'ember-qunit';
moduleForModel('item', 'Item Model', {
needs: ['model:item']
});
test('offer relationship', function() {
var relationships = Ember.get(App.Item, 'relationships');
deepEqual(relationships.get(App.Offer), [
{ name: 'offer', kind: 'belongsTo' }
]);
});
Run Code Online (Sandbox Code Playgroud)
错误跟踪:
Died on test #1 at test (http://localhost:4200/assets/vendor.js:73836:13)
at eval (goodcity/tests/unit/item-test.js:44:5)
at requireModule (http://localhost:4200/assets/vendor.js:54:29)
at http://localhost:4200/assets/test-loader.js:14:29: App is …Run Code Online (Sandbox Code Playgroud) 我有一个在localhost:4200上运行的Ember Cli项目和一个在localhost:56967上运行的asp.net webapi项目.两个项目分别运行良好:我可以启动我的Ember应用程序并测试几条路线,看起来很好,我可以访问我的api(例如:api/products),我看到了我的回复.
我遇到的问题是彼此挂起两件事.
适配器
export default DS.RESTAdapter.extend({
host: 'http://localhost:56967',
namespace: 'api'
});
Run Code Online (Sandbox Code Playgroud)
我第一次遇到一些Cors问题,但是我在我的Ember应用程序中修复了contentSecurityPolicy,并在我的Api上启用了Cors.
当我进入产品路线时,我可以看到对Api的请求被接受,Api回复了Json的回答.但是,我没有序列化模型,所以我可以在我的Ember应用程序中使用它.
这是我对Api的回应
[{"ProductId":1,"Name":"Product 1","Description":"Category 1"},{"ProductId":2,"Name":"Product 2","Description":"Category 2"},{"ProductId":3,"Name":"Product 3","Description":"Category 3"}]
Run Code Online (Sandbox Code Playgroud)
产品的Ember模型
export default DS.Model.extend({
name : DS.attr('string'),
description: DS.attr('string')
});
Run Code Online (Sandbox Code Playgroud)
Asp.net产品型号:
public class Product
{
public int ProductId { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我知道我必须序列化Api响应,使其成为我的Ember App的"可读"Json.现在的问题是:更改Api的格式是否更好?或者做一个好的序列化器?我如何制作序列化器?很难找到一些体面的教程.我尝试了这个,但这不起作用:
export default DS.RESTSerializer.extend({
primaryKey: 'productId'
});
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
Error while processing route: products No model was found for …Run Code Online (Sandbox Code Playgroud) 在一个ember 1.13.3应用程序中,我有这条路线:
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.store.find('skill');
}
});
Run Code Online (Sandbox Code Playgroud)
而这个型号:
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
link: DS.attr('string'),
acquired_skills: DS.hasMany('users', { async: true, inverse: 'acquired_skills' } ),
searched_skills: DS.hasMany('users', { async: true, inverse: 'searched_skills' } )
});
Run Code Online (Sandbox Code Playgroud)
这是api返回的结果:
{"skills":[{"id":1,"name":"Ember","description":"JS Framework","link":null}]}
Run Code Online (Sandbox Code Playgroud)
数组是空的.我可以用它检查它,console.log(this.store.find('skill').get('length'));我有0.
怎么了?
我在尝试从后端使用json时遇到此错误.我正在使用Ember CLI版本2.5.0和RestAdapter.
这是我的路线/ products/index.js看起来像:
export default Ember.Route.extend({
actions: {
[...]
},
model: function() {
return this.store.findAll('product');
}
});
Run Code Online (Sandbox Code Playgroud)
这就是我的json的样子:
{
"products":[
{
"id":9,
"name":"Product A",
"price_cents":1500,
"margin_cents":0,
"commission":0,
"expiration":null,
"track_stock":false,
"stock_amount":5,
"brand":{
"id":2,
"name":"SuperPet"
},
"group":{
"id":1,
"name":"Group A"
}
},
{
"id":8,
"name":"Product B",
"price_cents":1500,
"margin_cents":0,
"commission":0,
"expiration":null,
"track_stock":false,
"stock_amount":5,
"brand":{
"id":1,
"name":"Whiskas"
},
"group":{
"id":1,
"name":"Group B"
}
}
],
"meta":{
"pagination":{
"per_page":null,
"total_pages":4,
"total_objects":10
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据要求,这是模型:
import DS from 'ember-data';
const { attr, belongsTo } …Run Code Online (Sandbox Code Playgroud) 我试图过滤一些由海市蜃楼提供并有麻烦的Ember路线中的硬编码数据.
Ember呻吟我的语法(当试图filter在商店使用时)或当我使用时它不返回任何数据findAll,然后在对象上使用JS过滤器方法.
尝试1 - 使用findAll():
路线
export default Ember.Route.extend({
model() {
return {
txSites: this.get('store').findAll('site').filter((site) => {
return site.siteType === 'tx';
})
};
}
});
Run Code Online (Sandbox Code Playgroud)
模板
<select class="form-control">
{{#each model.txSites as |site|}}
<option value="{{site.id}}">{{site.name}}</option>
{{/each}}
</select>
Run Code Online (Sandbox Code Playgroud)
幻影终点
this.get('/sites', () => {
return {
data: [{
type: 'site',
id: 1,
attributes: {
name: 'London',
siteType: 'tx'
}
},
{
type: 'site',
id: 2,
attributes: {
name: 'Bristol',
siteType: 'rx'
}
}]
}
});
Run Code Online (Sandbox Code Playgroud)
结果
成功请求:GET/sites对象{data:Array [2]}
但没有任何限制下拉(其他调用我不试图过滤结果工作正常). …
我正在对具有DS.hasMany()关系属性的模型进行单元测试.每当我进行以下单元测试时,我都会在测试运行中遇到此错误:
Error: Assertion Failed: All elements of a hasMany relationship must be instances of DS.Model, you passed [<Ember.Object:ember367>,<Ember.Object:ember368>]
有人可以对此有所了解吗?
export default DS.Model.extend({
accounts: DS.hasMany('account'),
servicesAccounts: DS.hasMany('services-account'),
address: MF.fragment('address'),
appEligibilities: MF.fragmentArray('app-eligibility'),
appsForPremise: Ember.computed('accounts', function () {
return DS.PromiseArray.create({
promise: this.get('store').find('app', {
account: this.get('accounts').mapBy('id')
})
});
})
});
Run Code Online (Sandbox Code Playgroud)
import { moduleForModel, test } from 'ember-qunit';
import Ember from 'ember';
moduleForModel('premise', 'Unit | Model | premise', {
needs: [
'model:account',
'model:services-account',
'model:address',
'model:app-eligibility'
]
});
test('Apps for premise', function …Run Code Online (Sandbox Code Playgroud) 我正在尝试找出如何使用{{action}}帮助器根据select元素中的选择设置新的Ember Data Record的属性值。这是我的情况,简化了:
item.js路线。创建模板中可用的新记录。
model() {
return this.store.createRecord('item');
}
Run Code Online (Sandbox Code Playgroud)
new-item.hbs模板。选择元素触发操作以设置新项目的color属性。
<select {{action 'setColor' model value="target.value" on="change"}}>
<option value="">choose</option>
<option value="white">white</option>
<option value="blue">blue</option>
<option value="red">red</option>
<option value="yellow">yellow</option>
</select>
Run Code Online (Sandbox Code Playgroud)
item.js控制器。
actions: {
setColor(item, option) {
console.log(option);
record.set('color', option);
console.log(item);
}
}
Run Code Online (Sandbox Code Playgroud)
Console.log(option)不返回任何内容。record.set可以正常工作,我可以对选项进行硬编码,console.log(item)将显示新的item对象具有color属性设置。
另一方面,如果我只用
onChange={{action 'setColor' value="target.value"}}
Run Code Online (Sandbox Code Playgroud)
颜色将从操作中正确记录下来。但是,如果我像以前一样使用{{action ... on =“ change”}}语法在该动作调用中添加“模型”,console.log(option)返回未定义,而console.log(item)返回一个不带Event的事件。附加了模型对象。
问题是:我如何通过双方的target.value 和模型到的setColor行动?
ember-data ×10
ember.js ×10
ember-cli ×2
javascript ×2
unit-testing ×2
coffeescript ×1
ember-qunit ×1
filter ×1
html-select ×1
json ×1
qunit ×1