创建不从模板字段拉数据的新记录 [英] Creating a new record not pulling data from template fields

查看:109
本文介绍了创建不从模板字段拉数据的新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个新的记录,但是没有任何一个字段的数据被自动传递,正如我预期的那样,Ember(从我看过的)中。



我的模板:

 < form {{action save content on =submit}}> 
{{input value = name}}
< button type =submit}}> Next< / a>

从我所看到的内容

我的路线:

  App.CampaignsNewRoute = Ember.Route.extend({
actions:{
save:function(campaign){
console .log(campaign.name);
}
},

model:function(controller){
return this.store.createRecord('campaign');
}
});

我的控制器:

  App.CampaignsNewController = Ember.ObjectController.extend({
pageTitle:'New Campaign Setup'
});

当我点击下一步它记录 undefined 。只记录广告系列显示它是一个Ember模型,但没有名称属性。 名称广告系列模型中定义。将输入设置为 {{input value = content.name}} 名称属性放在返回的模型中,但仍然未定义。我在这个过程中遗漏了什么吗? EmberJS网站没有显示如何做,从我可以找到。



-



作为附注:我原来是使用 App.CampaignsNewController = Ember.Controller.extend ,因为我的模型正在返回承诺的哈希,其中之一这是一个数组,Ember不喜欢我使用数组对象控制器。我将其简化为上述,以验证不是导致问题的原因。所以任何解决这个问题的方案都是非常棒的。



编辑:我可以通过执行 .get('controller')。get('name')但肯定没有必要将我的控制器更改为 Ember.Controller.extend 也会阻止它的工作,很想知道为什么。澄清这里的最佳实践还是很棒的!



Edit2: this.get('controller.content') .get('name')如果控制器只是一个 Ember.Controller 而不是 Ember.ObjectController ,模板有 {{input value = content.name}} 。我会一起工作,但希望有人可以澄清这是正确的方法。

解决方案

ObjectController 是去这里的方式。您将拥有一个特定的模型,您的新模型的支持,并且您将添加额外的属性到控制器以用于模板。



代码



  App.IndexRoute = Ember.Route.extend({
actions:{
save:function(campaign){
console.log(campaign.get('color'));
}
},

model:function(){
return Ember.RSVP。 hash({
record:this.store.createRecord('color'),
all:this.store.find('color')
});
},
setupController:function(controller,model){
this._super(controller,model.record);
controller.set('allColors',model.all);
}
});

App.IndexController = Em.ObjectController.extend({

});



模板



您想要访问模型上的任何模型,您可以直接访问它,就像模型是当前范围一样。

  {{name}} 

如果要访问控制器上存在的任何属性,使用控制器上的属性名称。

  {{allColors.length}} 

这是一个例子:

  ; form {{action save model on =submit}}> 
颜色:{{input value = color}}< br />
< button type =submit> Next< / button>
< / form>

< ul>
{{#each item in allColors}}
{{#unless item.isNew}}
< li> {{item.color}}< / li>
{{/ unless}}
{{/ each}}
< / ul>



最后一个提示,总是使用getter和setter;)



Ember Data隐藏属性,它们不会在对象上生存,所以 campaign.name 将永远返回未定义。如果你执行 campaign.get('name'),你会得到一个真正的回应。



http://emberjs.jsbin.com/OxIDiVU/792/edit


I am attempting to create a new record, however none of the data from the fields is being passed automatically, as I expected Ember to (from what I've read).

My template:

<form {{action save content on="submit"}}>
{{input value=name}}
<button type="submit"}}>Next</a>

From what I've read content is an alias for model and interchanging these makes no difference.

My route:

App.CampaignsNewRoute = Ember.Route.extend({
  actions: {
    save: function(campaign) {
      console.log(campaign.name);
    }
  },

  model: function(controller) {
    return this.store.createRecord('campaign');
  }
});

And my controller:

App.CampaignsNewController = Ember.ObjectController.extend({
  pageTitle: 'New Campaign Setup'
});

When I hit 'Next' it logs undefined. Logging just the campaign shows it's an Ember model, but without the name attribute. name is defined on the campaign model. Setting the input to {{input value=content.name}} places the name attribute within the model returned, but it's still undefined. Am I missing anything in this process? The EmberJS site doesn't show how to do this, from what I can find.

--

As a side note: I was originally using App.CampaignsNewController = Ember.Controller.extend as my model was returning a hash of promises, one of which is an array and Ember didn't like me using either array or object controller. I simplified it to the above to verify it wasn't that which was causing the issue. So any solution taking this into account would be wonderful.

Edit: I can access the template fields by doing this.get('controller').get('name') but surely that is not necessary? Changing my controller to a Ember.Controller.extend also stops that from working, would love to know why. Clarification on best practice here would still be wonderful!

Edit2: this.get('controller.content').get('name') works if the controller is simply an Ember.Controller as opposed to Ember.ObjectController and the template has {{input value=content.name}}. I'll work with but hopefully someone can clarify this is the correct way.

解决方案

ObjectController is the way to go here. You would have it backed by one particular model, your new model, and you would add additional properties to the controller for use in the template.

Code

App.IndexRoute = Ember.Route.extend({
  actions: {
    save: function(campaign) {
      console.log(campaign.get('color'));
    }
  },

  model: function() {
    return Ember.RSVP.hash({
      record: this.store.createRecord('color'),
      all: this.store.find('color')
    });
  },
  setupController: function(controller, model){
    this._super(controller, model.record);
    controller.set('allColors', model.all);
  }
});

App.IndexController = Em.ObjectController.extend({

});

Template

In the template any time you want to access anything on the model backing the template, you can just access it as if the model is the current scope.

{{name}}

if you want to access any of the properties that exist on the controller you would use the property name that it is on the controller.

{{allColors.length}}

Here's an example:

<form {{action save model on="submit"}}>
    Color:{{input value=color}}<br/>
   <button type="submit">Next</button>
</form>

<ul>
  {{#each item in allColors}}
    {{#unless item.isNew}}
      <li>{{item.color}}</li>
    {{/unless}}
  {{/each}}
</ul>

One last tip, always use getters and setters ;)

Ember Data hides the properties, they don't live right on the object, so campaign.name will return undefined forever and ever. If you do campaign.get('name') you'll get a real response.

With the example: http://emberjs.jsbin.com/OxIDiVU/792/edit

这篇关于创建不从模板字段拉数据的新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆