EmberJS 测试观察器

2018-01-04 14:12 更新

描述

obsever用于观察计算属性的文本。

例子

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs Testing Observers</title>
         <!-- CDN's -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script>
      <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
      <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.prod.js"></script>
      <script src="https://code.jquery.com/qunit/qunit-1.18.0.js"></script>
      <script src="https://rawgit.com/rwjblue/ember-qunit-builds/master/ember-qunit.js"></script>
      <script src="https://builds.emberjs.com/release/ember.debug.js"></script>
      <script src="https://builds.emberjs.com/beta/ember-data.js"></script>
   </head>
<body>
   <div id="qunit"></div>
   <div id="ember-testing"></div>

   <script type="text/javascript">
      //Creates an instance of Ember.Application and assign it to a global variable
      App = Ember.Application.create();

     //To define a new Ember class, call the extend() method on Ember.Object
     App.MyFunc = Ember.Object.extend({
        name: 'Smith',
        prop: 'no',
        MyFunc: function(){
           this.set('prop', 'yes');
        }.observes('Name')
     });

    App.setupForTesting();
    App.rootElement = '#ember-testing';    //Ember.js applications's root element
    module('Emberjs');

    //Here, it tests the workflow of an application
    test('MyFunc observer sets prop property', function() {
       //'myfunc' is an instance of our class MyFunc
       var myfunc = App.MyFunc.create();

       //Update the observed property (Name), and assert that the expected effects are present
       myfunc.set('Name', 'Mack');
       equal(myfunc.get('prop'), 'yes');
    });
   </script>
</body>
</html>

输出

让我们执行以下步骤,看看上面的代码如何工作:

  • 将上面的代码保存在testing_observer.html文件中

  • 在浏览器中打开此HTML文件。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号