【问题标题】:Ember.js: replacing simple linkTo helper with a viewEmber.js:用视图替换简单的 linkTo 助手
【发布时间】:2013-02-12 04:13:15
【问题描述】:

我有一个内置了基本功能的应用程序。我不会经历并添加其他功能。在这种情况下,我需要将当前使用linkTo 的简单按钮转换为视图。问题是我不确定如何将一个转换为另一个并仍然保持链接完整。

如何进行这种转换?这是我现在拥有的代码:

<script type="text/x-handlebars" data-template-name="accountItem">
{{#each account in controller}}
    {{#linkTo "account" account}}
        <img {{bindAttr src="account.icon"}} />
    {{/linkTo}}
{{/each}}
</script>

这是我将拥有的代码:

<script type="text/x-handlebars" data-template-name="accountItem">
{{#each account in controller}}
    {{#view "Social.AccountButtonView"}}
        <img {{bindAttr src="account.icon"}} />
    {{/view}}
{{/each}}
</script>

Social.AccountButtonView = Ember.View.extend({
    tagName: 'a',
    classNames: ['item-account'],
    click: function(){
        // do something
    }
});

我假设我将在视图中的单击处理程序之上构建,但我不确定如何将引用传递给被迭代的项目,也不知道如何在视图中引用正确的路由。

请帮忙?

更新 1

第一个版本根据我设置的Router渲染一个值为#/accounts/4的href属性:

Social.Router.map(function() {
    this.resource('accounts', function(){
        this.resource('account', { path: ':account_id'});
    });
});

当我将当前代码转换为视图时,如何模仿 linkTo 提供的功能?

【问题讨论】:

    标签: ember.js handlebars.js


    【解决方案1】:

    您可以在车把模板中为account 定义属性绑定。 此绑定的工作方式如下:

    <script type="text/x-handlebars">
        <h1>App</h1> 
        {{#each item in controller}}
            {{#view App.AccountView accountBinding="item"}}
                <a {{bindAttr href="view.account.url"}} target="_blank">
                    {{view.account.name}}
                </a>
            {{/view}}
        {{/each}}
    </script>
    

    注意我加了accountBinding,所以一般规则是propertyNameBinding作为后缀。请记住,当您向视图添加属性时,您将无法直接访问它,而是必须使用view.propertyName 访问它,如上所示。

    请记住,在使用 {{view}} 助手时,您必须有一个 View 类:

    window.App = Em.Application.create();
    App.AccountView = Em.View.extend(); // this must exist
    App.ApplicationRoute = Em.Route.extend({
        model: function() {
            return [
                {id: 1, name: 'Ember.js', url: 'http://emberjs.com'}, 
                {id: 2, name: 'Toronto Ember.js', url: 'http://torontoemberjs.com'}, 
                {id: 3, name: 'JS Fiddle', url: 'http://jsfiddle.com'}];    
        }
    })
    

    工作小提琴:http://jsfiddle.net/schawaska/PFxHx/

    响应更新 1

    我发现自己处于类似情况,最终创建了一个子视图来模仿 {{linkTo}} 助手。我真的不知道/认为这是最好的实现。 你可以在这里看到我之前的代码:http://jsfiddle.net/schawaska/SqhJB/

    当时我已经在ApplicationView: 中创建了一个子视图:

    App.ApplicationView = Em.View.extend({
      templateName: 'application',
      NavbarView: Em.View.extend({
        init: function() {
          this._super();
          this.set('controller', this.get('parentView.controller').controllerFor('navbar'))
        },
        selectedRouteName: 'home',
        gotoRoute: function(e) {
          this.set('selectedRouteName', e.routeName);
          this.get('controller.target.router').transitionTo(e.routePath);
        },
        templateName: 'navbar',
        MenuItemView: Em.View.extend({
          templateName:'menu-item',
          tagName: 'li',
          classNameBindings: 'IsActive:active'.w(),
          IsActive: function() {
            return this.get('item.routeName') === this.get('parentView.selectedRouteName');
          }.property('item', 'parentView.selectedRouteName')
        })
      })
    });
    

    我的把手看起来像这样:

    <script type="text/x-handlebars" data-template-name="menu-item">
      <a {{action gotoRoute item on="click" target="view.parentView"}}>
      {{item.displayText}}
      </a>
    </script>
    
    <script type="text/x-handlebars" data-template-name="navbar">
      <ul class="left">
        {{#each item in controller}}
          {{view view.MenuItemView itemBinding="item"}}
        {{/each}}
      </ul>
    </script>
    

    很抱歉,我不能给你更好的答案。这是我当时能想到的,从那以后就再也没有碰过。就像我说的,我不认为这是处理它的方法。如果您愿意查看{{linkTo}} helper source code,您将看到一个模块化且优雅的实现,它可能是您自己实现的基础。我猜你正在寻找的部分是 href 属性,它的定义如下:

    var LinkView = Em.View.extend({
    ...
        attributeBindings: ['href', 'title'],   
        ... 
        href: Ember.computed(function() {
          var router = this.get('router');
          return router.generate.apply(router, args(this, router));
        })
    ...
    });
    

    所以我想,从那里您可以了解它的工作原理并自行实现某些东西。让我知道这是否有帮助。

    【讨论】:

    • 这开始变得更有意义,但它仍然没有解决我如何让链接工作......也许是因为我没有提供足够的前期信息。您介意查看更新 1 吗?
    猜你喜欢
    • 2013-04-14
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 2013-06-27
    • 2012-11-25
    • 2013-08-15
    相关资源
    最近更新 更多