【问题标题】:Ember.js - build a link dynamically using input valuesEmber.js - 使用输入值动态构建链接
【发布时间】:2013-10-23 12:34:24
【问题描述】:

使用 ember.js 我有一个输入:

{{input id="my_input" name="my_input" type="text"}}

然后我想创建一个链接,使用linkTo,但希望输入的值成为 href 的一部分。像这样:

{{#linkTo 'my_resource' my_input}}the link{{/linkTo}}

我有这样定义的资源:

App.Router.map(function() {
    this.resource("my_resource", {path: ":my_input"});
});

这可能吗?

谢谢。

【问题讨论】:

    标签: javascript ember.js link-to


    【解决方案1】:

    肯定的:

    http://emberjs.jsbin.com/eFAGixo/1/edit

    App.Router.map(function() {
      this.resource('search', {path:'search/:search_text'});
    });
    
    
    App.SearchRoute = Ember.Route.extend({
      model: function(params){
      // at this point params.search_text will have
      // what you searched for
      // since you are sending a string, it will hit the model
      // hook, had you sent an object it would have skipped this
      // hook and just used the object as your model
        return { searchInThisRoute: params.search_text};
      }
    });
    
    
    <script type="text/x-handlebars" data-template-name="application">
     <h2>Welcome to Ember.js</h2>
    
      {{input value=searchText}}
      {{#link-to 'search' searchText}} Go To Search{{/link-to}}
    
      {{outlet}}
    </script>
    

    PS:这实际上看起来像一个按钮和使用 Ember Actions 更有意义,但这就像花花公子一样工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多