EmberJS 對(duì)象模型鏈接計(jì)算屬性

2018-01-03 17:15 更新

描述

鏈接計(jì)算屬性用于與單個(gè)屬性下的一個(gè)或多個(gè)預(yù)定義計(jì)算屬性進(jìn)行聚合。

語句

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>

輸出

讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:

  • 將上面的代碼保存在obj_mod_chainin_cmp.html文件中

  • 在瀏覽器中打開此HTML文件。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)