2014-06-27 73 views
0

在Github上,介紹了許多把手助手。你可以找到它們here我如何在Ember中包含給定的把手助手

我想使用它們,但沒有想法,如何包含它們。代碼看起來像是javascript(文件以.js結尾......),但是像'import','export','default'這樣的詞令我感到困惑。

據我所知(猜測),我必須首先包括if-condition.js。之後,其他(包含)文件引用此文件。

但是當我這樣做,控制檯拋出Error:

未捕獲的SyntaxError:意外的保留字

有沒有人有想法,如何讓這些代碼工作?

回答

0

importexport是即將在下一個版本的Javascript中使用的module syntax的關鍵字。您今天可以使用它們,使用transpiler將其轉換爲正常的ES5語法。

但是,如果您只使用了一些幫助器,那麼手動「轉儲」它們非常容易。不要導出該函數,只需將它傳遞給Ember.Hanldebars.registerBoundHelper調用即可。這裏的if-condition幫手:

Ember.Handlebars.registerBoundHelper('if-condition', function() { 
    var args = [].slice.call(arguments); 
    var options = args.pop(); 
    var context = (options.contexts && options.contexts[0]) || this; 

    if (!options.conditional) { 
     throw new Error("A conditional callback must be specified when using the if-condition helper"); 
    } 

    // Gather all bound property names to pass in order to observe them 
    var properties = options.types.reduce(function(results, type, index) { 
     if (type === 'ID') { 
      results.push(args[index]); 
     } 
     return results; 
    }, []); 

    // Resolve actual values for all params to pass to the conditional callback 
    var normalizer = function() { 
     return Ember.Handlebars.resolveParams(context, args, options); 
    }; 

    // This effectively makes the helper a bound helper 
    // NOTE: 'content' path is used so that multiple properties can be bound to using the `childProperties` argument, 
    // however this means that it can only be used with a controller that proxies values to the 'content' property 
    return Ember.Handlebars.bind.call(context, 'content', options, true, options.conditional, normalizer, properties); 
}); 
+0

包括JS作品,螺母使用模板中的助手,拋出一個錯誤: 「未捕獲錯誤:斷言失敗:registerBoundHelper生成傭工不支持與把手塊使用。 「 – lukkysam

+0

我發現這個:[link](http://emberjs.jsbin.com/zelib/1/edit)。 與您的代碼非常相似。這工作正常。 – lukkysam

+0

不幸的是,我不得不取消標記,問題得到解答。 如果我使用「registerHelper」,它們工作正常,直到屬性更改。然後,該視圖不會更新。似乎,就像我必須使用「registerBoundHelper」,但使用它,發佈的錯誤將發生。 – lukkysam