Ember数据this.store得到undefine

时间:2014-06-23 09:34:50

标签: ember.js ember-data

我想使用emberdata.Js添加新记录但不能正常工作  这是我的代码

<!DOCTYPE html>
<html>
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="ED: Reading" />

<script src="js/jquery-1.10.2.js"></script>
<script src="js/handlebars-1.1.2.js"></script>
<script src="js/ember-1.5.1.js"></script>
<script src="http://builds.emberjs.com/beta/ember-data.prod.js"></script>



<script type="text/javascript">

App = Ember.Application.create();

App.Store = DS.Store.extend({
revision: 12,
url: 'http://localhost/Ember/Demo2/'
});

App.Pull = DS.Model.extend({
title: DS.attr(),
url: DS.attr(),
});

App.Router.map(function(){
this.resource('pull');
});

var store = this.store;

//var obj = App.Pull.createRecord();



App.PullRoute = Ember.Route.extend({
model: 
    function() {
        store.createRecord('pull', {
title: 'Rails is Omakase',
url: 'Lorem ipsum'
});
    //return this.store.find('pull');
    //return App.Pull.find();
    //this.store.createobjects(response);
    }
});




</script>

</head>
<body>

<script type="text/x-handlebars">
 <h1>Welcome</h1>
 <div class="navbar-header">
      {{#link-to 'pull' classNames='navbar-brand'}}
        City List
      {{/link-to}} 
    </div>
 {{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="pull">
 <h2>GetAllCityList</h2>
 <div class="navbar-header">
 <ul>
 {{#each model}}
    <li>{{title}}</li>
 {{/each}}
 </ul>
 </div>

</script>



</body>
</html>

我试图在模型城市的调用中添加记录到商店类型的模型但是this.store给了我undefined。

下面的

是ember insepctor中的错误

Error while loading route: TypeError: Cannot read property 'createRecord' of undefined
at App.PullRoute.Ember.Route.extend.model   (http://localhost/Ember/Demo2/storedemo.html:42:9)
at superWrapper [as model] (http://localhost/Ember/Demo2/js/ember-1.5.1.js:1292:16)
at Ember.Route.Ember.Object.extend.deserialize (http://localhost/Ember/Demo2/js/ember-1.5.1.js:36570:19)
at http://localhost/Ember/Demo2/js/ember-1.5.1.js:32972:57
at http://localhost/Ember/Demo2/js/ember-1.5.1.js:33464:19
at invokeResolver (http://localhost/Ember/Demo2/js/ember-1.5.1.js:9646:9)
at new Promise (http://localhost/Ember/Demo2/js/ember-1.5.1.js:9632:9)
at Router.async (http://localhost/Ember/Demo2/js/ember-1.5.1.js:33463:16)
at Object.HandlerInfo.runSharedModelHook (http://localhost/Ember/Demo2/js/ember-1.5.1.js:32971:16)
at Object.UnresolvedHandlerInfoByParam.getModel (http://localhost/Ember/Demo2/js/ember-1.5.1.js:33058:19)

>this.store
undefined

4 个答案:

答案 0 :(得分:1)

仅供参考Ember models docs

&#34;商店对象在控制器和路由中可用,使用this.store&#34;

我遇到了这个错误,因为我试图从component.js文件访问商店。

来自Josh Farrant here

的有关此问题的实用文章

答案 1 :(得分:0)

您可以使用以下方式创建记录:

...
var obj = store.createRecord('pull', {
    title: 'Rails is Omakase',
    url: 'Lorem ipsum'
});    
obj.save().then(function(record){
    return record;
});

答案 2 :(得分:0)

看起来像是一个范围问题。您正在分配&#39; var store = this.store;&#39;在您的应用程序范围内,但Ember.js根据您对Ember.Route的扩展定义创建新对象,因此&#39; store&#39;在App.PullRoute中可能与它出现的不同。

试试这个:

App.PullRoute = Ember.Route.extend({
model: function() {
        this.store.createRecord('pull', {
          title: 'Rails is Omakase',
          url: 'Lorem ipsum'
          });
     }
});

答案 3 :(得分:0)

我没有看到任何问题。当我尝试时,我确实让商店归还了。我刚刚创建了一个jsbin,以防你想检查。

http://emberjs.jsbin.com/zesemuqe/1/edit

我确实发现了两个问题。你确定要创建一条记录,将其送入商店。但是你没有回来。

App.PullRoute = Ember.Route.extend({
    model: function() {
        return this.store.createRecord('pull', {
            title: 'Rails is Omakase',
            url: 'Lorem ipsum'
        });
    }
});

2.其次,我可以看到你试图遍历不是数组而只是一个对象的模型,因此把手会对此产生错误。

<script type="text/x-handlebars" data-template-name="pull">
    <ul>
        <li>{{title}}</li>
    </ul>
</script>

我知道这肯定不是问题,但你是否正确加载了所有依赖项,jsbin缺少它们?

相关问题