【问题标题】:Ember.js Strip Binding TagsEmber.js 剥离绑定标签
【发布时间】:2012-01-10 01:50:09
【问题描述】:

有没有办法从 ember.js 注入的车把模板中去除绑定标签?我希望能够只提取没有任何变形 script 标记的 html。

我有这个related question,但也想问这个更笼统的问题。

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    您可以使用 unbound Handlebars 助手在单个属性级别执行此操作。

    正在对 #unbound 块帮助器进行工作,这对于您正在尝试做的事情会很好:https://github.com/emberjs/ember.js/pull/321

    另一种方法是在您的视图中指定一个普通的 Handlebars 模板。不会绑定任何输出。

    App.UnboundView = Ember.View.extend({
      template: Handlebars.compile("output is: {{msg}} here"),
      msg: "not bound"
    });
    

    这是一个 jsFiddle 示例:http://jsfiddle.net/ebryn/zQA4H/

    【讨论】:

      【解决方案2】:

      这是一个更好的方法

      {{unbound propertyName}}
      

      http://emberjs.com/api/classes/Ember.Handlebars.helpers.html#method_unbound

      【讨论】:

        【解决方案3】:

        如果有人需要这个功能,我创建了一个小的 jquery 插件来做到这一点:

        # Small extension to create a clone of the element without
        # metamorph binding tags and ember metadata
        
        $.fn.extend
          safeClone: ->
            clone = $(@).clone()
        
            # remove content bindings
            clone.find('script[id^=metamorph]').remove()
        
            # remove attr bindings
            clone.find('*').each ->
              $this = $(@)
              $.each $this[0].attributes, (index, attr) ->
                return if attr.name.indexOf('data-bindattr') == -1
                $this.removeAttr(attr.name)
        
            # remove ember IDs
            clone.find('[id^=ember]').removeAttr('id')
            clone
        

        仍然希望有更好的方法。

        【讨论】:

        • 不知道新版Ember有没有更好的办法?
        猜你喜欢
        • 2014-03-16
        • 1970-01-01
        • 2013-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-27
        • 2023-03-17
        相关资源
        最近更新 更多