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


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


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

ArrayProxy 的 cacheFor() 方法用於獲取計算屬性的緩存值(如果存在)。此方法幫助我們在不調用屬性的情況下檢查計算屬性的值。

句法:

this.object.cacheFor( keyName );

參數:

  • keyName: 它是我們想要其計算屬性值的屬性的名稱。

返回:它返回計算屬性的緩存值(如果存在)。

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

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

npm install ember-cli

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

ember new <project-name> --lang en

要啟動服務器,請鍵入:

ember serve

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

ember generate route test1

應用程序/路線/test1.js

Javascript


import Route from '@ember/routing/route'; 
import { computed } from '@ember/object'; 
import ArrayProxy from '@ember/object/proxy'; 
  
const Employee = ArrayProxy.extend({ 
    toStringExtension() { 
        return this.get('name'); 
    }, 
    fullName: computed('firstName', 'lastName', 
        function () { 
            return this.get('firstName') + ' '
                + this.get('lastName'); 
        }) 
}); 
  
export default class DetailsRoute extends Route { 
    person; 
    createPerson() { 
        let person = Employee.create({ 
            firstName: 'Sam', 
            lastName: 'Snehil', 
            age: 32, 
            mobile: '837483837847', 
            country: 'India', 
            language: 'Tamil', 
            salary: 34000, 
            gender: 'M', 
            zipCode: '476384', 
        }); 
        return person 
    } 
  
    model() { 
        this.person = this.createPerson(); 
        return this.person 
    } 
    setupController(controller, model) { 
        super.setupController(controller, model); 
        controller.set('person', this.person); 
    } 
} 

應用程序/控製器/test1.js

Javascript


import Ember from 'ember'; 
import EmberObject from '@ember/object'; 
  
export default Ember.Controller.extend({ 
  
    actions: { 
        store() { 
            let temp = this.person.cacheFor('fullName'); 
            console.log(temp + ' Cached From ' + 
                this.person.toString()) 
        } 
    } 
});

應用程序/模板/test1.hbs

HTML


{{page-title "ArrayProxy"}} 
<table> 
    <h1>Employee's Details: </h1> 
    <div><b>Name:</b> {{this.person.fullName}}</div> 
    <div><b>Gender:</b> {{this.person.gender}}</div> 
    <div><b>Country:</b> {{this.person.country}}</div> 
    <div><b>Salary:</b> {{this.person.salary}}</div> 
    <div><b>Zip Code:</b> {{this.person.zipCode}}</div> 
    <div><b>Language Speaks:</b>{{this.person.language}}</div> 
    <div><b>Mobile No.:</b>{{this.person.mobile}}</div> 
    <div><b>Age:</b> {{this.person.age}}</div> 
</table> 
  
<br /><br/> 
<input
    type="button"
    id="Cache"
    value="Cache the Property"
    {{action 'store'}} 
> 
{{outlet}}

輸出:

緩存用於輸出1

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

ember generate route test2

應用程序/路線/test2.js

Javascript


import Route from '@ember/routing/route'; 
import ArrayProxy from '@ember/object'; 
import { computed } from '@ember/object'; 
  
const Person = ArrayProxy.extend({ 
    Grade: computed('marks', function () { 
        let temp = this.get('marks') 
        return temp > 80 ? 'A' : temp > 70 ? 'B' : 
            temp > 60 ? 'C' : 'D'; 
    }), 
    toStringExtension() { 
        return this.get('name'); 
    }, 
}); 
  
export default class StudentsRoute extends Route { 
    person = Person.create({ 
        name: 'Umesh dubey', 
        gender: 'M', 
        class: 10, 
        marks: 83, 
        mobile: '8399384948', 
        Roll_No: '1029394', 
        Branch: 'Commerce', 
    }) 
  
    model() { 
        return this.person; 
    } 
    setupController(controller, model) { 
        super.setupController(controller, model); 
        controller.set('person', this.person); 
    } 
}

應用程序/控製器/test2.js

Javascript


import Ember from 'ember'; 
import EmberObject from '@ember/object'; 
import { cacheFor, destroy } from '@ember/object'; 
  
export default Ember.Controller.extend({ 
    actions: { 
  
        storeNonCompute() { 
            let temp = this.person.cacheFor('name'); 
            console.log(temp + ' is Cached From ' + 
                this.person.toString()) 
        }, 
        storeCompute() { 
  
            let temp = this.person.cacheFor('Grade'); 
            console.log(`'${temp}'` + ' Grade is Cached2 From ' + 
                this.person.toString()) 
        }, 
  
    } 
}); 

應用程序/模板/test2.js

HTML


{{page-title "ArrayProxy"}} 
<table> 
    <h1>Student's Details:</h1> 
    <div><b>Name:</b> {{this.person.name}}</div> 
    <div><b>Gender:</b> {{this.person.gender}}</div> 
    <div><b>Class:</b> {{this.person.class}}</div> 
    <div><b>Mobile:</b> {{this.person.mobile}}</div> 
    <div><b>Grade:</b> {{this.person.Grade}}</div> 
    <div><b>Marks:</b> {{this.person.marks}}</div> 
    <div><b>Branch:</b> {{this.person.Branch}}</div> 
      
  
</table> 
<br /><br/> 
<input
    type="button"
    id="CacheFor"
    value="Cache the Non Computed Property"
    {{action 'storeNonCompute'}} 
/><br /><br/> 
<input
    type="button"
    id="CacheFor"
    value="Cache the computed Property"
    {{action 'storeCompute'}} 
/> 
{{outlet}}

輸出:

緩存輸出2

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



相關用法


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