OKU*_*UZA 1 javascript ember.js
我一直在研究一个模仿这个emberjs教程的ember模板.但是,我不想为属性使用相同的对象,而是指定此操作来自的"项目"对象.它们存储在一个通过调用模型访问的数组中.当我将对象作为特定操作的参数传递时,我可以记录该对象并查看它是否已定义.但是,当我尝试设置或获取属性时,我收到错误:
Uncaught TypeError: undefined is not a function
Run Code Online (Sandbox Code Playgroud)
您可以看到我尝试使用的两种方法来解决错误,但是,每次调用仍然会导致错误.
方法一:
contract: function(project){
var indx = this.projects.indexOf(project)
console.log(indx);
this.projects[indx].set('isExtended', false);
}
Run Code Online (Sandbox Code Playgroud)
方法二:
contract: function(project){
project.set('isExtended', false);
}
Run Code Online (Sandbox Code Playgroud)
我认为问题与存储在模型或项目中的数组有关.如果有人有任何建议如何纠正这种类型的错误,请告诉我.提前谢谢,这里是所有相关代码:
HTML:
<script type="text/x-handlebars" id="projects">
<div class = "row bodeh">
<div class ="col-lg-12">
<h1>Projects</h1>
</div>
{{#each project in model}}
<div {{bind-attr class=project.sass style=project.image}}>
<div class="cont">
<h1><a {{bind-attr href=project.lynk}}>{{project.title}}</a></h1>
<p>{{project.body}}</p>
</div>
{{#if project.isExtended}}
<div class="extraMat">
<button type="button"{{action 'contract' project}}>Contract</button>
<p>{{project.content}}</p>
</div>
{{else}}
<button type="button"{{action 'expand' project}}>Expand</button>
{{/if}}
</div>
{{/each}}
</div>
</script>
Run Code Online (Sandbox Code Playgroud)
EmberJS
App.ProjectsRoute = Ember.Route.extend({
projects: [],
actions: {
expand: function(project) {
console.log(this.projects.indexOf(project));
project.set('isExtended', true);
},
contract: function(project){
var indx = this.projects.indexOf(project)
console.log(indx);
this.projects[indx].set('isExtended', false);
}
},
model: function() {
var objects = [];
//to handle the array less thing with foreach, just add index as a property. Count is also valuable.
var tuna = {
bgurl: "img/tunadbo.jpg",
title: "Tuna",
body: "Tuna is a music discovery application. But is it really? TONIGHT, we investigate. ",
content: "This is the content"
};
var greatest = {
bgurl: "img/great.png",
title: "Greatest",
body: "You best believe",
content: "This is the content"
};
var sus = {
bgurl: "img/combatix.png",
title: "Combatix",
body: "Small video game\n",
content: "This is the content"
}
var trust = {
bgurl: "img/great.png",
title: "The Trustiest of the trust\n",
body: "Ya know",
content: "This is the content"
}
objects.pushObject(tuna);
objects.pushObject(greatest);
objects.pushObject(sus);
objects.pushObject(trust);
for(i = 0; i< objects.length; i++)
{
objects[i].lynk = "http://tunadbo.appspot.com";
objects[i].image = "background-image:url('"+objects[i].bgurl+"');";
objects[i].isExtended = true;
objects[i].sass = "col-lg-12 col-sm-12 col-xs-12";
objects[i].sass += " heedthis hvr-fade";
}
this.projects = objects;
return objects;
}
});
Run Code Online (Sandbox Code Playgroud)
根据@ Beerlington的答案更新JSBin http://emberjs.jsbin.com/davabonoto/2/edit
你得到的错误是因为普通的JavaScript对象不理解Ember的get/set函数.有两种方法可以解决这个问题.
1)您可以将每个对象文字包装在Ember.Object.create:
这是我正在谈论的一个例子:
var tuna = Ember.Object.create({
bgurl: "img/tunadbo.jpg",
title: "Tuna",
body: "Tuna is a music discovery application. But is it really? TONIGHT, we investigate. ",
content: "This is the content"
});
Run Code Online (Sandbox Code Playgroud)
2)您可以继续使用对象文字而不是Ember.set在对象上调用它:
expand: function(project) {
Ember.set(project, 'isExtended', true);
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
526 次 |
| 最近记录: |