我正在尝试从路径加载一个ember-data用户模型,而不是可以接受用户名或id.
我修改了RESTAdapter find方法以将JSON响应中的id传递给didFindRecord,但没有任何内容传递给我的视图.似乎Ember仍在尝试使用用户名作为模型的ID.
我的路由器看起来像这样:
App.Router = Em.Router.extend({
location: 'history'
, root: Em.Route.extend({
user: Em.Route.extend({
route: '/users/:slug'
, connectOutlets: function(router, context) {
router.get('applicationController').connectOutlet('user', context)
}
, serialize: function(router, context) {
return { slug: context.get('username') || context.get('id') }
}
, deserialize: function(router, params) {
return App.User.find(params.slug)
}
})
})
})
Run Code Online (Sandbox Code Playgroud)
我的find方法如下所示:
var CustomAdapter = DS.RESTAdapter.extend({
find: function(store, type, id) {
var root = this.rootForType(type)
this.ajax(this.buildURL(root, id), 'GET', {
success: function(json) {
this.didFindRecord(store, type, json, json[root].id)
}
})
}
})
App.store …Run Code Online (Sandbox Code Playgroud) 回滚父记录似乎不会删除可能已添加到其中的任何新子记录.不确定这是否是错误或功能.
App.Company = DS.Model.extend({
name: DS.attr('string'),
employees: DS.hasMany('App.Employee')
});
App.Employee = DS.Model.extend({
name: DS.attr('string'),
company: DS.belongsTo('App.Company')
})
var company = App.Company.find(1);
//Initially: company.get('employees.length') => 0
var employee = company.get('employees').createRecord();
//Now: company.get('employees.length') => 1
employee.get('transaction').rollback();
//Still: company.get('employees.length') =>1
//Also: employee.get('isDeleted') => true
company.get('transaction').rollback();
//Even now: company.get('employees.length') =>1
//How do I rollback such that company.get('employees.length') == 0
Run Code Online (Sandbox Code Playgroud) 在Ember数据0.13中,复数定义如下:
DS.RESTAdapter.configure("plurals", {
category: "categories"
});
Run Code Online (Sandbox Code Playgroud)
配置不再存在,因此需要另一种方式来定义复数.
通过魔术,似乎如果我通过"返回this.store.find('category');"进行查找,则JSON调用包括/ categories而不是/ categorys,尽管我还没有完全指定类别的复数是类别......
Ember数据如何确定这一点?有办法覆盖吗?
谢谢
我正在尝试使用ember.js和ember-data创建一个应用程序,使用以下版本:
DEBUG: Ember : 1.7.0
DEBUG: Ember Data : 1.0.0-beta.9
DEBUG: Handlebars : 1.2.1
DEBUG: jQuery : 2.1.0
Run Code Online (Sandbox Code Playgroud)
我正在使用RESTAdapter连接到我使用node.js编写的api.
加载应用程序后,我不断收到以下错误:
Error while processing route: students undefined is not a function TypeError: undefined is not a function
at http://localhost:9000/scripts/vendor/ember-data.js:12006:34
at tryCatch (http://localhost:9000/scripts/vendor/ember.js:45818:16)
at invokeCallback (http://localhost:9000/scripts/vendor/ember.js:45830:17)
at publish (http://localhost:9000/scripts/vendor/ember.js:45801:11)
at http://localhost:9000/scripts/vendor/ember.js:29069:9
at DeferredActionQueues.invoke (http://localhost:9000/scripts/vendor/ember.js:634:18)
at Object.DeferredActionQueues.flush (http://localhost:9000/scripts/vendor/ember.js:684:15)
at Object.Backburner.end (http://localhost:9000/scripts/vendor/ember.js:147:27)
at Object.Backburner.run (http://localhost:9000/scripts/vendor/ember.js:202:20)
at apply (http://localhost:9000/scripts/vendor/ember.js:18382:27)
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的代码(以我粘贴的相同顺序加载):
app.js
var App = window.App = Ember.Application.create({
LOG_ACTIVE_GENERATION: true,
LOG_TRANSITIONS: true,
LOG_TRANSITIONS_INTERNAL: false,
LOG_VIEW_LOOKUPS: true …Run Code Online (Sandbox Code Playgroud) 我在ember-cli应用程序中有这些模型:
var PuzzleRound = DS.Model.extend({
year: DS.attr('number')
});
var Puzzle = DS.Model.extend({
puzzleRounds: DS.hasMany('puzzleRound', {async: true})
});
Run Code Online (Sandbox Code Playgroud)
这是我的测试tests/unit/models/puzzle-test.js:
import {
moduleForModel,
test
} from 'ember-qunit';
import PuzzleRound from 'weather-roulette/models/puzzle-round';
moduleForModel('puzzle', 'Puzzle', {
// Specify the other units that are required for this test.
needs: ['model:puzzleRound']
});
test('it exists', function() {
var model = this.subject();
// var store = this.store();
ok(!!model);
});
Run Code Online (Sandbox Code Playgroud)
运行时出现此错误ember test:
Attempting to register an unknown factory: `model:puzzleRound`
Run Code Online (Sandbox Code Playgroud)
我使用的是ember-cli 0.1.1,Ember.js 1.7.0,Ember Data 1.0.0-beta.11.有没有人可以尝试解决这个问题?
我正在编写我的第一个Ember应用程序,而这一刻,我正在尝试从我的API(使用Rabl在Rails中制作)中使用JSON,但RESTAdapater无效.它甚至没有伸出我的服务器!我得到了这段代码:
应用程序/适配器/ application.js中
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
host: 'localhost:3000',
namespace: 'api'
});
Run Code Online (Sandbox Code Playgroud)
应用程序/模型/ player.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
heightFormatted: DS.attr('string'),
heightCm: DS.attr('number'),
weightLb: DS.attr('number'),
weightKg: DS.attr('string'),
birthplace: DS.attr('string'),
birthdate: DS.attr('string'),
neoId: DS.attr('number'),
position: DS.attr('string'),
number: DS.attr('string')
});
Run Code Online (Sandbox Code Playgroud)
应用程序/路由/播放/ index.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.store.find('player');
}
});
Run Code Online (Sandbox Code Playgroud)
应用程序/路由/ players.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function(params) {
return this.store.find('player', params.player_id);
}
});
Run Code Online (Sandbox Code Playgroud)
谁有想法?我想我已经妥善安排了所有人.
CONSOLE.LOG …
我正在使用:
ember-cli 0.2.7ember-data 1.0.0-beta.18ember 1.12.0我不知道为什么,但似乎我无法检索我的newsletter模型的标签.
我正在使用ActiveModelAdapter:
import DS from 'ember-data';
export default DS.ActiveModelAdapter.extend({
namespace: 'api/v1',
host: 'http://localhost:3000'
});
Run Code Online (Sandbox Code Playgroud)
newsletter.js
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
tags: DS.hasMany('tag')
});
Run Code Online (Sandbox Code Playgroud)
tag.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string')
});
Run Code Online (Sandbox Code Playgroud)
API响应(使用ActiveModelSerializer的rails后端):
{
"newsletters": [
{
"id": 1,
"title": "Panel Weekly",
"tag_ids": [
1
]
},
{...}
],
"tags": [
{
"id": 1,
"name": "arts"
},
{...}
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何检索新闻稿的标签列表.我使用ember检查器和控制台($ E包含第一个时事通讯)尝试了这个:
>$E.get('tags.length')
0 …Run Code Online (Sandbox Code Playgroud) 我是一个非常快速聚集在一起的开源团队的一员.我们都使用预算编制的应用程序,它总是一个桌面应用程序.他们刚从头开始发布了一个完整的重写,现在使用Ember应用程序在网上发布.
当然,与任何重写一样,许多功能都没有为发布做好准备,而且高级用户非常沮丧.我个人喜欢某些不存在的功能,但不是对此抱怨而已,我决定只使用Chrome扩展程序添加我想要的功能.
事实证明这很受欢迎,但我们现在所做的主要是微小的表面更新和CSS黑客攻击.当我们想要注入DOM元素或类似元素时,我们依赖于这样的脆弱的hacks:
(function checkCreditBalances() {
if ( typeof Em !== 'undefined' && typeof Ember !== 'undefined' && typeof $ !== 'undefined' && $('.is-debt-payment-category.is-master-category').length ) {
// Do stuff here with thing
}
}
setTimeout(checkCreditBalances, 300);
)();
Run Code Online (Sandbox Code Playgroud)
当我阅读Ember文档时,它总是讨论在构建实际应用程序时要做什么.我不是,所以在创建应用程序时调试标志对我没有帮助.我没有构建模板等等.Ember检查员不认识它是一个ember应用程序,因为它已经是一个生产版本.
我需要能够更深入地(但安全地)挂钩到应用程序中以添加更多高级功能.所以:
我们可以抓住任何内部钩子来告诉我们何时渲染组件,它是什么,并且如果它是我们想要的那个,我们有机会将DOM元素注入其中?或者替换实例化时使用的模板?或者添加事件处理程序?
是否有一种干净的方式来告诉应用程序何时加载,我们可以将我们的逻辑注入顶部?
有没有办法替换整个应用程序的某个组件?这个特殊的应用似乎在窗口上定义了Em和Ember,我不知道为什么,因为两者似乎都拥有非常相似的对象.
有没有一种方法可以将点击处理程序(或一般的事件处理程序)附加到组件上?
如何从Ember外部与Ember Data进行交互?它是一样的,还是应该记住什么?我知道我可以使用Ajax调用进行复制,但我假设Ember Data正在进行一些缓存和其他优点,并希望利用应用程序已经在后端进行的调用(如果可能的话).
基本上,任何人都可以向我提供有关如何从Chrome扩展程序向Ember应用程序注入内容的任何指示吗?
谢谢!
我想在API请求导致404错误时采取特定操作.我已经读过,执行此操作的适当方式是处理应用程序适配器中的错误,如下所示:
handleResponse: function(status, headers, payload){
if(status === 404 && payload.errors){
//handle error
}
return this._super(...arguments);
}
Run Code Online (Sandbox Code Playgroud)
问题是,一旦我设置了适配器,它就不会完成加载页面,所以我可以处理页面本身的错误.相反,它会自动转到一些错误路径,只是说"适配器错误".如何停止/覆盖此行为?
更新:
有人可以帮忙吗?本周好一半,我一直在追求这个没有好运的事情.我注意到客户端正在生成两个POST.我已经为适配器添加了代码.还有其他我应该看的吗?
我正在浏览下面提供的视频教程,当我单击"提交"按钮将数据保存到数据库时,无法解决两个错误.
我看到数据库有两个新记录.当我再次单击提交按钮时,应用程序会将我带回到todo-items页面,其中显示了两个记录.谁能告诉我我做错了什么?
当前版本:
视频教程(错误发生在11:30标记处):https://www.youtube.com/watch?v = bZ1D_aYGJnU.注意:该视频的作者似乎已经将重复的POST问题在视频结尾处消失了,但我看不出如何.
组件/表格/ TODO项表格/ component.js
import Component from '@ember/component';
export default Component.extend({
actions:{
save(){
this.get('submit')();
}
}
});
Run Code Online (Sandbox Code Playgroud)
组件/表格/ TODO项表格/ template.hbs
<form {{action "save" on="submit"}}>
{{input placeholder="description" value=todoItem.description}}
<br />
{{#if todoItem.validations.isValid}}
<button type="submit">Add</button>
{{else}}
<button type="submit" disabled>Add</button>
{{/if}}
</form>
Run Code Online (Sandbox Code Playgroud)
模板/ S/TODO项/ add.hbs
{{forms/todo-item-form
todoItem=model
submit=(route-action "submitAction")
}}
{{outlet}}
Run Code Online (Sandbox Code Playgroud)
型号/待办事项,item.js
import DS from 'ember-data';
import { validator, buildValidations } from 'ember-cp-validations'; …Run Code Online (Sandbox Code Playgroud)