當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Embeer.js Component cacheFor()用法及代碼示例


Ember.js 是一個開源 JavaScript 框架,用於開發基於 Model-View-Controller (MVC) 架構的大型客戶端 Web 應用程序。 Ember.js是使用最廣泛的前端應用框架之一。它的目的是加速開發並提高生產力。目前,它被大量網站使用,包括 Square、Discourse、Groupon、Linked In、Live Nation、Twitch 和 Chipotle。

Component 方法的 cacheFor() 方法用於獲取計算屬性的緩存值(如果存在),否則調用計算屬性。

句法:

this.object.cacheFor( PropertyName );

參數:

  • PropertyName: 我們想要其存儲緩存值的計算屬性的名稱。

返回:它返回計算屬性的緩存值。

安裝和運行 Ember.js 的步驟:

步驟 1:要運行以下示例,您需要有一個 ember 項目。要創建一個,您需要先安裝ember-cli。在終端中寫入以下代碼:

npm install ember-cli

第 2 步:現在,您可以通過輸入以下代碼來創建項目:

ember new <project-name> --lang en

要啟動服務器,請鍵入:

ember serve

示例 1:鍵入以下代碼以生成本示例的路由:

ember generate route temp1

應用程序/組件/second.js

Javascript


import Component from '@glimmer/component'; 
import Ember from 'ember'; 
import EmberObject from '@ember/object'; 
import { action, without, set, get, computed } from '@ember/object'; 
  
const Student = EmberObject.extend({ 
    fullName: computed('firstName', 'lastName', { 
        get() { 
            return `${this.firstName} ${this.lastName}`; 
        } 
    }), 
  
    toStringExtension() { 
        return this.get('fullName'); 
    }, 
}); 
export default Ember.Component.extend({ 
    students: [ 
        Student.create({ 
            firstName: 'Sam', 
            lastName: 'Snehil', 
            Marks: 72, 
            class: 11, 
        }), 
        Student.create({ 
            firstName: 'Ram', 
            lastName: 'Sahu', 
            Marks: 84, 
            class: 10, 
        }), 
        Student.create({ 
            firstName: 'Soham', 
            lastName: 'Verma', 
            Marks: 69, 
            class: 12, 
        }), 
        Student.create({ 
            firstName: 'David', 
            lastName: 'Tigga', 
            Marks: 53, 
            class: 9, 
        }), 
        Student.create({ 
            firstName: 'Pokhu', 
            lastName: 'Verma', 
            Marks: 95, 
            class: 10, 
        }), 
        Student.create({ 
            firstName: 'Satyam', 
            lastName: 'Verma', 
            Marks: 75, 
            class: 12, 
        }), 
    ], 
  
     @action 
    StoreName(data) { 
  
         this.students.forEach(function (item) { 
            if (item.fullName == data) { 
  
            let temp = item.cacheFor('fullName'); 
            console.log(`${temp} is stored from ${item.toString()}`) 
  
        } 
    }) 
} 
    }) 

應用程序/組件/second.hbs

HTML


{{page-title "Component cacheFor"}} 
<h3>List of Item in Buckets</h3> 
<table> 
    <tr> 
        <th> Name </th> 
        <th>Class </th> 
        <th>Marks </th> 
    </tr> 
    {{#each this.students as |temp|}} 
    <tr> 
        <td>{{temp.fullName}}</td> 
        <td>{{temp.class}}</td> 
        <td>{{temp.Marks}}</td> 
    </tr> 
    {{/each}} 
</table> 
<br /> 
<div> 
    <label>Enter Name : </label> 
    {{input value=this.item}} 
</div><br /> 
  
<input type="button"
       id="Cache-property" 
       value="Store the Computed Property"
      {{action "StoreName" this.item}} /> 
<br /><br /> 
{{outlet}}

應用程序/模板/temp1.hbs

HTML


<Second> 
     Example of Components cacheFor method 
</Second>

輸出:

輸出1

示例 2:在下麵的示例中,我們將看到計算屬性的惰性。每當調用計算屬性時,我們都會在控製台中看到輸出。當我們檢索屬性的緩存值時,會看到計算屬性未被調用。

鍵入以下代碼以生成本示例的路由:

ember generate route temp2

應用程序/組件/first2.js

Javascript


import Component from '@glimmer/component'; 
import Ember from 'ember'; 
import EmberObject from '@ember/object'; 
import { action, without, set, get, computed} from '@ember/object'; 
  
let value = 0;  
const Student = EmberObject.extend({ 
    describeSkill: computed('Name', 'skill',{ 
        get() { 
                console.log('Computed property called for ', this.Name, value+=1) 
                return `${this.Name} knows ${this.skill} language`; 
              } 
            }), 
  
    toStringExtension() { 
                return this.get('Name'); 
            }, 
    }); 
export default Ember.Component.extend({ 
    students : [ 
        Student.create({ 
            Name: 'Balit', 
            skill: 'Python', 
            Id: 'stu2', 
          
        }), 
        Student.create({ 
            Name: 'Arabh', 
            skill: 'c++', 
            Id: 'stu5', 
        }), 
        Student.create({ 
            Name: 'Tanu', 
            skill: 'Java', 
            Id: 'stu4', 
        }), 
        Student.create({ 
            Name: 'Pokhu', 
            skill: 'JavaScript', 
            Id: 'stu3', 
        }), 
        Student.create({ 
            Name: 'Sam', 
            skill: 'R', 
            Id: 'stu1', 
        }), 
        Student.create({ 
            Name: 'Permu', 
            skill: 'PHP', 
            Id: 'stu0', 
        }), 
    ], 
  
    @action 
    StoreName(data) { 
        this.students.forEach( function(item){ 
                if(item.Name == data ){ 
  
                    item.describeSkill 
                    let temp = item.cacheFor('describeSkill'); 
                    console.log(`'${temp}' is stored from ${item.toString()}`) 
  
                }  
        }) 
    } 
  
    })

應用程序/組件/first2.hbs

HTML


{{page-title "Component cacheFor"}} 
<h3>List of Item in Buckets</h3> 
<table> 
    <tr> 
        <th> Name </th> 
        <th>Skill </th> 
        <th>Id </th> 
    </tr> 
    {{#each this.students as |temp|}} 
    <tr> 
        <td>{{temp.Name}}</td> 
        <td>{{temp.skill}}</td> 
        <td>{{temp.Id}}</td> 
    </tr> 
    {{/each}} 
</table> 
<br /> 
<div> 
    <label>Enter Name : </label> 
    {{input value=this.item}} 
</div><br /> 
  
<input
    type="button"
    id="Cache-property"
    value="Store the Computed Property"
    {{action "StoreName" this.item}} 
/> 
<br /><br /> 
{{outlet}} 

應用程序/模板/temp2.js

HTML


<First> 
    Example of Component cacheFor method 
</First>

輸出:在輸出中我們可以看到計算屬性僅被調用一次。

輸出2

參考:https://api.emberjs.com/ember/4.9/classes/Component/methods/cacheFor?anchor=cacheFor



相關用法


注:本文由純淨天空篩選整理自jeemains0neet大神的英文原創作品 Ember.js Component cacheFor() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。