ember.js - Why should I use Ember.run() when handling events in other libraries? -
looking @ ember.js documentation, rather vague when make use of ember.run()
(my emphasis).
normally should not need invoke method yourself. if implementing raw event handlers when interfacing other libraries or plugins, you should wrap of code inside call.
from looking through source , reading posts on topic, understanding when call ember.run()
, following occurs.
- the given callback run.
- the run loop algorithm executed, flushing queues , ensuring bindings synchronized, etc.
i'm trying understand why recommended handle other libraries' events inside of call ember.run()
. take following example create jquery ui slider , handle slide
event.
<script type="text/x-handlebars" data-template-name="index"> <div id="slider"></div> <h2>{{value}}</h2> <h3>{{valueprose}}</h3> </script> app = ember.application.create(); app.indexcontroller = ember.controller.extend({ actions: { valuechange: function (newvalue) { this.set('value', newvalue); } }, valueprose: function () { var value = this.get('value'); if (ember.isnone(value)) { return ''; } if (value <= 25) { return 'pretty small'; } if (value <= 50) { return 'getting bigger'; } if (value <= 75) { return 'whoa watch out'; } if (value < 100) { return 'can\'t handle it!'; } return 'ahhhgghuarghblarp!'; }.property('value') }); app.indexview = ember.view.extend({ didinsertelement: function () { var controller = this.get('controller') this.$('#slider').slider({ min: 0, max: 100, slide: function (event, ui) { // ember.run(function () { controller.send('valuechange', ui.value); // }); } }); } });
here working fiddle: http://jsfiddle.net/ahaurw01/kwze5/2/
whether or not slide
event handled inside of call ember.run()
, expected behavior occurs. i'd understand optimizations or insurance offers. either way, see value
getting set , valueprose
updated appropriately. 1 of cases documentation talking when suggesting code wrapped in ember.run()
?
i believe idea of running ember.run
async events, such event listeners 3rd party libraries, can synced ember run loop.
i learned ton ember.run , why it's useful when started integrating qunit project, because when start testing app, automatic ember.run loop feature code taken out , it's you.
Comments
Post a Comment