简体   繁体   English

在ember.js中共享不同模板的按钮

[英]Share buttons for different templates in ember.js

I have multiple models each of which has its own template. 我有多个模型,每个模型都有自己的模板。 I want to share a footer of buttons accross the different templates so when the same button is pressed different functions are executed according to the selected template. 我想在不同模板上共享按钮的页脚,以便在按下同一按钮时根据所选模板执行不同的功能。 The HTML looks like this: HTML看起来像这样:

    <script type="text/x-handlebars" id="application" >
       <menu>
          <nav>
             <ul>
               <li>{{#link-to 'model1'}}Model 1{{/link-to}}</li>
               <li>{{#link-to 'model2'}}Model 2{{/link-to}}</li>
               <li>{{#link-to 'model3'}}Model 3{{/link-to}}</li>
             </ul>
          </nav>
       </menu>

       <div class="content">
         <section>
           {{outlet}}
         </section>
       </div>

      <footer>
        <ul>
          <li><button class="action-undo" {{action undo}}>Undo</button></li>
          <li><button class="action-save" {{action save this}}>Save</button></li>
          <li><button class="action-saveContinue" {{action saveAndContinue this}}>Save & Continue</button></li>
        </ul>
      </footer>
    </script>

You probably want to use the {{partial}} helper. 您可能想使用{{partial}}助手。 It will inline a template (with the same context) when it is called. 调用时,它将内联模板(具有相同的上下文)。

To use the partial helper just create a template with a leading underscore, like : _buttons . 要使用部分帮助器,只需创建一个带有下划线的模板,例如: _buttons To include that template you can now use {{partial "buttons"}} . 要包括该模板,您现在可以使用{{partial "buttons"}}

See: http://emberjs.com/guides/templates/rendering-with-helpers/ 请参阅: http : //emberjs.com/guides/templates/rendering-with-helpers/

You can keep the application template with the footer so all templates inserted in the outlet will have the footer. 您可以将应用程序模板保留在页脚中,以便插入插座的所有模板都具有页脚。

To get the actions triggering to some corresponding template, just override the route of that template, for example in the model1 template, you can create a App.Model1Route implementing the actions: 要获取触发某个相应模板的动作,只需覆盖该模板的路由,例如在model1模板中,您可以创建一个App.Model1Route实现这些动作:

App.Model1Route = Ember.Route.extend({
    actions: {
        undo: function() {
            // perform undo
        },
        save: function() {
            // perform save
        },
        saveAndContinue: function() {
            // perform save and continue
        }
    }
});

And for model2 template just override the App.Model2Route and add the actions to be triggered in that template, and so on. 对于model2模板,只需重写App.Model2Route并在该模板中添加要触发的动作,依此类推。

Give a look in that fiddle to see this working http://jsfiddle.net/marciojunior/d3xA6/ 看一看小提琴,看看能正常工作的http://jsfiddle.net/marciojunior/d3xA6/

Be careful because if some route don't implement some triggered action, an error is raised. 请注意,因为如果某些路由未执行某些触发的操作,则会引发错误。 In that case you can extract your footer to a partial and just add where is used. 在这种情况下,您可以将页脚提取为partial然后仅添加使用位置。

I hope it helps 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM