当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。