未加星标

Ember.js Components with DOM Dependencies

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Ember.js is a great framework for building single-page applications. Its mantra of “data down, actions up” sets a clear guideline on how to structure most of your application.

Your route gets some data and tees it up for your controller/component to render it. When something changes the UI (say entering text), you fire an action with the updated values and use it to update your data. This approach also helps with rendering speed under the hood. Your view is totally driven by your model. Perfect. Unless…

What happens when things aren’t so cut and dry? Let’s walk through a case where part of the view depends on how another part renders.

Recently, I had to build a matching question form that required drawing lines between matched elements. I wanted to float an SVG over the parent element and position the lines based on the items on each side of the match. But since Ember components don’t have access to each other, how would I know when it’s ready?

First, a couple of basic handlebars templates:

<div class="leftCol">
{{#each leftItems as |left|}}
{{connectable-item item=left }}
{{/each}}
</div>
<div class="rightCol">
{{#each rightItems as |right|}}
{{connectable-item item=right }}
{{/each}}
</div>
{{magic-connection-lines pairs=pairs}} <svg>
{{#each matchPairs as |mp|}}
<line x1={{mp.x1}} y1={{mp.y1}} x2={{mp.x2}} y2={{mp.y2}} />
{{/each}}
</svg>

We let the parent component wrangle jQuery and wait for our children to be done rendering:

import Ember from 'ember';
export default Ember.Component.extend({
isPollingForItems: false,
didInsertElement(){
this._super(...arguments);
Ember.run.next(this, this.get('pollForItemsRendered'));
},
didUpdateAttrs() {
// look again if our model has changed
Ember.run.next(this, this.get('pollForItemsRendered'));
},
_isElementLoaded($el) {
let isLoaded = true;
if($el && $el.length > 0) {
var imgs = $el.find('img');
for(var i = 0; i < imgs.length; i++){
if(!imgs[i].complete){
isLoaded = false;
}
}
} else {
isLoaded = false;
}
return isLoaded;
},
pollForAnswersRendered(){
var allLoaded = true;
this.get('items').forEach(item => {
var $item = Ember.$(`[data-id='${item.id}']`);
if(!this._isElementLoaded($item)) { allLoaded = false; }
});
if(allLoaded){
this.set('items', Ember.merge({}, this.get('items')));
} else {
Ember.run.next(this, this.get('pollForAnswersRendered'));
}
},
leftItems: Ember.computed('items', function() {
this.get('items').filterBy('left').map(item => {
var $item = Ember.$(`[data-id='${item.id}']`);
return {
x1: $item.position().left,
y1: $item.position().top,
...
};
});
}),

So, we’ve managed to wire up our top-level component to:

Use jQuery to get the positions of previously rendered DOM elements Wait for all images to load; poll if they are not Restart the process if our underlying model changes

You can definitely feel that we’re going against the eloquent grain of Ember, but we still have the flexibility to make things work.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: jQuery
分页:12
转载请注明
本文标题:Ember.js Components with DOM Dependencies
本站链接:http://www.codesec.net/view/482934.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(25)