Ember添加检查转换呼叫上下文(路由/控制器)

时间:2016-04-10 15:46:21

标签: javascript ember.js

在我的Ember应用程序中,我有一个常见的mixin / code,它有一个转换调用

myCommonFn: function() {
//this.transition('someRoute') // Works if context is route
//this.transitionTo('someRoute') // Works if context is controller
}

现在因为此功能在多个路径中很常见,所以可以从控制器或路径

触发

我的问题是如何确保这两种情况都有效?我应该使用

添加一个检查
this.constuctor() //Check if it is controller or route

OR 我应该对

进行空/未定义检查
this.transition() OR this.transitionToRoute()

在所有情况下使其运行的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

如果你的mixin适用于所有地方,最简单的方法就是检查并开火。此外,如果您有升级计划,最终控制器将会消失,因此这可能只是一个临时修复。

<!DOCTYPE html>
<html>

<head>
  <title>Our Company</title>
  <link href="site.css" rel="stylesheet">
</head>

<body>

  <div id="main">
    <h1>E-commerce Website</h1> 
    <h2>Website Pages:</h2>

    <p>Home Page</p>
    <p>About Us</p>
    <p>Our Products</p>
    <p>Buy Now</p>
    <footer id="foot01"></footer>
  </div>

  <script src="script.js"></script>

</body>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
  <input type="hidden" name="cmd" value="_xclick">
  <input type="hidden" name="business" value="Email">
  <input type="hidden" name="lc" value="GB">
  <input type="hidden" name="button_subtype" value="services">
  <input type="hidden" name="no_note" value="0">
  <input type="hidden" name="currency_code" value="GBP">
  <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">
  <input type="image" src="https://www.paypalobjects.com/en_US/GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
  <img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>

</html>

您还可以将常用mixin扩展两次,并具有基本版本,控制器版本和路由版本,您可以在其中指定路由/控制器特定功能。