这让我抓狂。我或多或少地遵循http://emberjs.com/guides/的指南视频,但提供了我自己的夹具数据数据集。但是,无论我尝试什么,我最终都会遇到错误:
未捕获的类型错误:无法读取未定义的 ember-data.js 的属性“promise”:240
JS:
App.Store = DS.Store.extend({
revision: 12,
adapter: 'DS.FixtureAdapter'
});
App.UnauthedRoute = Ember.Route.extend({
model: function() {
return App.CurrentUser.find();
}
});
App.CurrentUser = DS.Model.extend({
username: DS.attr('string'),
email: DS.attr('string'),
session: DS.attr('string')
});
App.CurrentUser.FIXTURES = [
{
id: 1,
username: "gamerbob",
email: "bob@gamer.com",
session: "ABC123"
}
];
Run Code Online (Sandbox Code Playgroud)
模板:
<script type="text/x-handlebars" data-template-name="unauthed">
Resusult:
{{#each model}}
{{username}}
{{/each}}
</script>
Run Code Online (Sandbox Code Playgroud)
如果我从路线的模型挂钩中删除 App.CurrentUser.find() ,则不会出现错误,但这也达不到目的。这里发生了什么?我做错了什么,还是余烬数据不稳定?
在升级到ember-data-1.0.0-beta.9之后,我注意到我的应用程序由于对单个项目提出了很多请求而运行得慢得多:
而不是使用?ID在beta.8中对它们进行批处理:
GET /api/comments/?ids[]=1&ids[]=2&....
Run Code Online (Sandbox Code Playgroud)
我错误地认为这个PR已经进入beta.9并且我可以fetchBatchSize在RESTAdapter上设置参数,但是没有......
如何在beta.9中获得类似的批处理行为?
我想知道为什么我的模板createRecord在findQuery用于获取数据后没有得到更新.
将此更改return this.store.findQuery('timetracking', {year: year, month: month, user_id: user_id});为return this.store.find('timetracking');模板时,将使用我的新记录进行更新.
我不想获取所有记录以保存带宽,但是当仅find/findQuery使用查询参数时,我新创建的记录不会显示在我的模板中.
我是否必须进行"强制"重装?怎么做?
更新
灰烬检查员显示新记录.
我有一个customer模型和一个product模型
客户模型
import DS from 'ember-data';
export default DS.Model.extend({
type: DS.attr('string'),
name: DS.attr('string'),
product: DS.hasMany('product')
});
Run Code Online (Sandbox Code Playgroud)
产品型号
import DS from 'ember-data';
export default DS.Model.extend({
type: DS.attr('string'),
name: DS.attr('string'),
customer: DS.belongsTo('customer')
});
Run Code Online (Sandbox Code Playgroud)
我将所有客户加载model()到 route.js 的钩子中,如下所示
model() {
customers: this.store.findAll('customer'),
}
Run Code Online (Sandbox Code Playgroud)
所以到目前为止我拥有客户的所有记录。
然后使用findRecord方法我将特定产品加载为
this.store.findRecord('product', productId);
Run Code Online (Sandbox Code Playgroud)
我把这种对click一个的button发言权查找产品,这是写在controller.js
现在当我在模板中写成
{{product.customer.name}}
Run Code Online (Sandbox Code Playgroud)
它发送一个获取请求来加载已经加载的客户。我想停止该请求,我该怎么做?
据我所知
当我们已经有了记录时,store.findRecord 将立即使用缓存数据解析,但会在后台发送请求以更新记录,一旦记录更新,您的模板将显示新的更改。
我想停止那个请求。
我的后端总是用所有可用的数据进行响应,这花费了相当多的时间。所以我定期重新加载商店,我计划使用 peekAll() 和 peekRecord()。
我的代码是:
model: function() {
return Ember.RSVP.hash({
'clusters': this.store.peekAll('cluster'),
'single': this.store.peekRecord('cluster', 'cluster::My')
});
Run Code Online (Sandbox Code Playgroud)
执行代码时,起初我可以看到这两个项目都不包含内容。几秒钟后,数据被加载到存储中,我可以按预期在模板上看到内容“集群”。但是 'single' 仍然完全没有内容({{model.single}} 在模板中不返回任何内容)。但是当我有一个带动作的按钮时:
alert(this.store.peekRecord('cluster', 'cluster::My'));
Run Code Online (Sandbox Code Playgroud)
我可以看到记录已找到。记录也可通过 Ember Inspector 获得。我做错了什么,只有 peekAll() 适合我的模型。
在我的 ember.js 项目中,我使用 Typescript 和checkJs选项键入 check javascript 文件。
这是一个简单的route.js文件示例
import Route from '@ember/routing/route';
export default class UsersRoute extends Route {
model() {
return this.store.findAll('user');
}
}
Run Code Online (Sandbox Code Playgroud)
使用此代码,我收到以下 Typescript 错误
route.js:5:31 - error TS2345: Argument of type '"user"' is not assignable to parameter of type 'never'.
5 return this.store.findAll('user');
~~~~~~
Run Code Online (Sandbox Code Playgroud)
ember-data 类型定义由 @types/ember-data 包提供,这里是findAll 定义
findAll<K extends keyof ModelRegistry>(
modelName: K,
options?: {
reload?: boolean;
backgroundReload?: boolean;
include?: string;
adapterOptions?: any;
}
): PromiseArray<ModelRegistry[K]>;
Run Code Online (Sandbox Code Playgroud)
这就是我在调查如何定义时可以走多远的地方findAll。 …
有没有办法访问嵌入式模型对象的父对象?例如 :
App.Person = DS.Model.extend({
name : DS.attr('string'),
emails : DS.hasMany('App.Email', { embedded: true })
});
App.Email = DS.Model.extend({
label : DS.attr('string'),
email : DS.attr('string'),
setParentUpdated: function() {
if(this.get('isDirty') == true)
// this.get('parent').get('stateManager').goToState('updated');
// I would like to do something like this.get('parent')
// to access 'App.Person' instance object
}.observes('isDirty')
});
Run Code Online (Sandbox Code Playgroud) 假设以下型号:
App.User = DS.Model.extend(
email: DS.attr('string')
session_users: DS.hasMany('sessionUser')
)
App.SessionUser = DS.Model.extend(
user: DS.belongsTo('user')
state: DS.attr('string')
session: DS.belongsTo('session')
)
App.Session = DS.Model.extend(
title: DS.attr('string')
session_users: DS.hasMany('sessionUser')
)
Run Code Online (Sandbox Code Playgroud)
路线session_route.js.coffee:
App.SessionRoute = Ember.Route.extend(
model: (params) ->
this.store.find('session', params.id)
)
Run Code Online (Sandbox Code Playgroud)
以下session.hbs模板:
{{#each session_users}}
{{state}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)
我连接到WebSocket流,当创建一个新的SessionUser时,我收到通知.
这里的sessions_controller.js.coffee用于测试推送一些有效负载:
payload =
id: 20
user: controller.store.getById('user', 2)
session: controller.store.getById('session', 2)
state: 'confirmed'
controller.store.push('sessionUser', payload)
Run Code Online (Sandbox Code Playgroud)
使用Ember Inspector(Chrome扩展程序)我可以看到会话用户被推送并且存在于具有正确关系的商店中,但模板未更新.
当使用store.createRecord时,模板实际上正在更新,但我想使用push/pushPayload以便我可以使用现有的序列化程序.
我使用网站上提供的宝石设置了Ember js的Rails 4应用程序
的Gemfile
gem 'ember-rails'
gem 'ember-source', '1.2.0'
Run Code Online (Sandbox Code Playgroud)
entries_controller.js.coffee
Tut1.EntriesController = Ember.ArrayController.extend
addEntry: ->
entry = @store.createRecord(Tut1.Entry,
name: @get('newEntryName')
winner: false
)
entry.save()
Run Code Online (Sandbox Code Playgroud)
我在控制台上收到此错误.
POST http://localhost:3000/entries 422 (OK)
Run Code Online (Sandbox Code Playgroud)
它正确发布,但rails重新调整了一个"ActionController :: InvalidAuthenticityToken",这让我感到困惑,因为主机,起源和引用是相同的.
Host:localhost:3000
Origin:http://localhost:3000
Referer:http://localhost:3000/
Run Code Online (Sandbox Code Playgroud)
它仍然是跨域的吗?我如何验证此请求.
我刚刚开始使用ember,并开始使用一个ember.js应用程序,该应用程序从一个运行JSON REST API插件的Wordpress博客加载它的数据存储.
API的命名空间为"wp-json.php",并使用这些约定.
问题一:
我Error while loading route尝试设置DS.RESTAdapter并加载模型后出现错误.
问题二:
我正在尝试传递标题"type": "custom_post_type_slug",以获取自定义帖子类型的数据,并且无法使其工作.
这是我的代码:
App = Ember.Application.create({ }); //Create App
//Router
App.Router.map(function () {
this.resource('posts', {path: '/'}, function() {
this.resource('post', { path: ':post_id' } );
});
}); //End Router
//Posts Route
App.PostsRoute = Ember.Route.extend({
model: function(){
return this.store.find('post');
},
});
//Data
App.Post = DS.Model.extend({
title : DS.attr('string'),
});
App.PostAdapter = DS.RESTAdapter.extend({
host: 'http://example.com',
namespace: 'wp-json.php'
});
DS.RESTAdapter.reopen({
headers: {
"type": "custom_post_type_slug" …Run Code Online (Sandbox Code Playgroud)