codecamp

EmberJS 对象模型链接计算属性

描述

链接计算属性用于与单个属性下的一个或多个预定义计算属性进行聚合。

语句

App.ClassName = Ember.Object.extend({
   NameOfComputedProperty1: function() {
      return VariableName;
   }.property('VariableNames','...','VariableNamesn'),

   NameOfComputedProperty2: function() {
      return VariableName;
   }.property('NameOfComputedProperty1', 'VariableNames','...','VariableNamesn')
});

例子

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs Chaining Computed Properties</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 Person and Details computed property function
            Person: function() {
               return this.get('firstName') + ' ' + this.get('lastName');
            }.property('firstName', 'lastName'),

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

         var emp = App.Person.create({
         //initializing the values for variable
            firstName: 'Jhon',
            lastName: 'K',
            age: 24,
            mobno:'8792227920'
         });
         document.write("Details of employee: <br>");
         document.write(emp.get('Details'));//displaying the values by get() method
      </script>
   </body>
</html>

输出

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

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

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

温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }