EmberJS 对象模型动态更新

2018-01-03 17:18 更新

描述

Ember.js的set()方法有助于在调用计算属性时动态更新计算属性。

语句

ClassName.set('VariableName', 'UpdatedValue');

例子

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs Dynamic Updating</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://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script>
      <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.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>
      <script type="text/javascript">
         App = Ember.Application.create();

         App.Person = Ember.Object.extend({
            firstName: null,
            lastName: null,
            age: null,
            mobno: null,

            //defining the 'Details' as computed property fucntion
            Person: function(){
               return this.get('firstName') + ' ' + this.get('lastName');
            }.property('firstName', 'lastName'),

            //defining the 'Details' as computed property fucntion
            Details: function() {
               return 'Name: '+this.get('Person') + ' Age: ' + this.get('age') + ' Mob No: ' + this.get('mobno');
            }.property('Person', 'age', 'mbno')
         });

         //initializing the Person details
         var emp = App.Person.create({
            //Dynamically Updating the properties
            firstName: 'Manu',
            lastName: 'DK',
            age: 24,
            mobno:'8792227920'
         });

         //setting the updated value for 'firstName'
         emp.set('firstName', 'Jhon');
         document.write("Details of employee:
"+emp.get('Details'));//dynamically updated values
     </script>
   </body>
</html>

输出

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

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

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

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

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号