最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to delete or set null the contents of an Ember object? - Stack Overflow

programmeradmin0浏览0评论

I am using Ember.js for my project, and there is a point when I have to remove all the contents or set every property to "" or null for an Ember.Object.

So what is happening is, within the template, there are handlebars tags that link to some of the objects properties ({{myProperty}}),so when the object is emptied or every property set to "" or null, this binding should still exist if the properties are updated to new values.

Is there a way to achieve this? Is there a way to maybe loop through all the properties quickly?

I am using Ember.js for my project, and there is a point when I have to remove all the contents or set every property to "" or null for an Ember.Object.

So what is happening is, within the template, there are handlebars tags that link to some of the objects properties ({{myProperty}}),so when the object is emptied or every property set to "" or null, this binding should still exist if the properties are updated to new values.

Is there a way to achieve this? Is there a way to maybe loop through all the properties quickly?

Share Improve this question edited Jan 8, 2013 at 13:35 CraigTeegarden 8,2618 gold badges39 silver badges43 bronze badges asked Jan 7, 2013 at 17:33 justkashjustkash 7092 gold badges13 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

(started writing this thinking that eachAttribute is a method on Ember.Object, but it's on DS.Model instead. So...)

If your object happens to be an Ember Data DS.Model instance, there is the eachAttribute method, which takes a callback function. So one way to do what you're suggesting might be:

modelobj.eachAttribute(function(propName){
    modelobj.set(propName, null);
});

The only other idea that springs to mind is instantiating a new "blank" instance of your object, and assigning it in the old one's place...I think handlebars bindings would update properly in most cases if you did that.

Would it work for your application to use an {{#if}} statement in the template to conditionally show/hide your properties?

Like this:

{{#if view.showStuff }}
    <p>{{view.myProperty1}}</p>
    <p>{{view.myProperty2}}</p>
    <p>{{view.myProperty3}}</p>
{{else}}
    <p>Not showing content</p>
{{/if}}

If showStuff is true then your properties will be rendered, otherwise it will display the other html.

Example http://jsfiddle/cteegarden/rHafx/1/

发布评论

评论列表(0)

  1. 暂无评论