本文介绍了ember js在DOM if语句中比较值,或至少在DOM中具有值的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果语句 {{#if value =='otherValue'}} ,我一直在尝试比较句柄中的某些值,显然不成功,因为句柄不喜欢这个,期望一个字符串,布尔或函数名。那么这样可以,但是我尝试传递函数中的参数,就像你可以使用 {{action}} 助手一样,而且没有锻炼,



错误:断言失败:您必须只传递一个参数到if帮助器



所以我决定在一个视图中这样做,即使这样,ember js指出访问范围内的模板值是不寻常的,他们只提供不好的段落,没有例子。



所以当我试图这样做,我有一个访问这些变量的问题,我尝试这样的方式 this.get('controller.templateVariables')并通过查看的完整路径,但值是未定义或 .get()不存在作为一种方法。



所以在这一刻,我决定保存DOM数据属性中的变量,但是将这个 {{#查看App.TabsView data-title ={{tab}}}} 将会逐渐给我一个字符串 {{tab}} 当我尝试从视图访问 this.get('data-title')



剩下的唯一方法是在其中插入附加元素,并在其中存储变量,然后使用jQuery类选择器进行访问。但是在 isVisible 函数执行时,DOM中不存在元素,所以我当时没有访问值。这就解释了为什么 this.get('element')正在返回 null



类似于ember js的例子主要是以$ code> if(someLogic){} 为例,但是当没有可用变量时,我可以做什么逻辑对我来说



问题



为了简化我的问题 - 有没有办法我怎么能做这样的事情在ember js?简单的

  //将对象存储在控制器
var obj = [{title:'Tab1'},{标题:'Tab2'},{title:'Tab3'}];

//通过它们在DOM中循环
obj.forEach(function(tab){
//做这种比较
if(tab.title = = currentTab()){
//在这里做某事
}
});

如果不可能,那么实现类似功能的另一种方式是什么? >

解决方案

我认为我最好的方式来证明这一点,我用大量评论的JSFiddle,我已经为你提供了一些:



请问



主要部分决定何时添加活动 class在计算属性中:

  //确定我们对此视图的对象是否与activeTab对象相同。如果它是相同的,那么这个视图是当前的活动选项卡。 
active:function(){
return Boolean(this.get('parentView.activeTab')== this.get('tab'));
} .property('parentView.activeTab')


I been trying to compare some values in handlebars if statement {{#if value == 'otherValue'}}, obviously unsuccessfully because handlebars do not like this and expecting a string, boolean, or function name. Well that would be ok, but then I tried to pass parameter in the function like you can do with {{action}} helper, and well that didn't workout either, got this in console

Error: assertion failed: You must pass exactly one argument to the if helper

So then I decided to do this in a View, even so ember js guides points that accessing template values in-scope is unusual and they provide only poor paragraph with no examples.http://emberjs.com/guides/understanding-ember/the-view-layer/#toc_accessing-template-variables-from-views

So when I tried to do this, I got a problem of accessing those variables, I tried this way this.get('controller.templateVariables') and via full path to View, but value was either undefined or .get() wasn't exists as a method.

So at this moment I decided to save variable in the DOM data property, but turns out this {{#view App.TabsView data-title="{{tab}}"}} is going to literately give me a string {{tab}} when I try to access it from View with this.get('data-title').

The only way left to me was to insert additional element inside view and store variable there, and afterwards access it with jQuery class selector. but element is not yet exist in the DOM at the time of isVisible function gets executed, so I have no access to values at that time. That explains why this.get('element') was returning null.

Similar examples on ember js mostly ends up with something like if (someLogic) {}, but how I can do any logic when there is no variables available to me.

Question

To simplify my question - is there a way how I can do such a thing in ember js? Simple as

// have objects stored in controller
var obj = [{title:'Tab1'}, {title:'Tab2'}, {title:'Tab3'}];

// loop via them in the DOM
obj.forEach(function(tab) {
    // do this kind of comparison
    if( tab.title == currentTab() ) {
        // do something here
    }
});

If that is not possible, then what would be the other way to achieve similar functionality?

解决方案

I think the best way for me to demonstrate this is with a heavily commented JSFiddle that I've put together for you: http://jsfiddle.net/PbLnm/

Please ask any questions below if you're not sure about anything.

The main part which determines when to add the active class is in the computed property:

// Determine if the object we have for this view is the same as the activeTab's object. If it is the same, then this view is the current active tab.
active: function() {
    return Boolean(this.get('parentView.activeTab') == this.get('tab'));
}.property('parentView.activeTab')

这篇关于ember js在DOM if语句中比较值,或至少在DOM中具有值的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 01:26