Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ember.run.once running several times when method is an anonymous function #12369

Closed
Leooo opened this issue Sep 18, 2015 · 8 comments
Closed

Comments

@Leooo
Copy link
Contributor

Leooo commented Sep 18, 2015

I'm not good for jsfiddles, but the below is pretty clear:

``` javascript````
export default Ember.Controller.extend({
..
_test1: function(){
var arr=[1,1,1,1,1,1,1,1,1];
var self=this;
arr.forEach(function(i){
Ember.run.once(self,'_testlog');
});
},
_testlog: function(){
console.log("_testlog");
},
_test2: function(){
var arr=[1,1,1,1,1,1,1,1,1];
var self=this;
arr.forEach(function(i){
Ember.run.once(self,function(){
this._testlog();
});
});
},
});


``` javascript
  test1() //=> "_teslog"
  test2() //=> "_teslog,_teslog,_teslog,_teslog,_teslog,_teslog,_teslog,_teslog,_teslog,_teslog,_teslog"

So this behaviour is very confusing and doesn't fit with the docs: http://emberjs.com/api/classes/Ember.run.html#method_once

tested with Ember 1.12.1.

note:
arr and the forEach loop have no impact here, I just created them to show a clear example

note2:
if I want to use a function as a method for Ember.run.once, it is because I want to nest an Ember.run.next inside it without writing still another CP for the Ember.run.next: there is no Ember.run.onceNext method available, while I tend to use it everywhere in my app (this is different than Ember.run.scheduleOnce('destroy',..)

note3:
I had the same kind of confusing behavior with store.filter, where if I remember well I had in that case to use a function for the method, and not a property string to make it work.

@stefanpenner
Copy link
Member

When passing an anonymous function to once, it receives a new function each time. This prevents the once check from being possible, since it is getting a legitimate new function.

I believe this is covered in the run-loop guide. If not be sure to open an issue on the guide/website repo

@Leooo
Copy link
Contributor Author

Leooo commented Sep 19, 2015

That totally makes sense, but the docs should be updated.

@stefanpenner
Copy link
Member

looks like the issue was introduced emberjs/website@ddbc9bc

@stefanpenner
Copy link
Member

opened an issue, with suggestions for the fix. emberjs/guides#776

Maybe you have some free cycles to test your new undestanding of once and related functions. I can gladly give feedback on the submission

@stefanpenner
Copy link
Member

Also for reference, the API docs are accurate: http://emberjs.com/api/classes/Ember.run.html#method_scheduleOnce

@stefanpenner
Copy link
Member

extra comment to ensure no confusion i the API docs: #12373

@Leooo
Copy link
Contributor Author

Leooo commented Sep 19, 2015

Thanks, in the API docs http://emberjs.com/api/classes/Ember.run.html#method_scheduleOnce I would have just replaced method Function | String with method String. For testing this, I would do it had I time for learning Ember (and Rails) testing framework and testing my own website first.. :( I know you are overburdened with everything, the day I am less suffocated I will certainly add my fork of EmberSync / LocalStorage adapter this may be useful to some (locks/ember-localstorage-adapter#105 is very big issue on LS I believe, especially for new guys who are testing Ember's performance wih the LS adapter).

@Leooo
Copy link
Contributor Author

Leooo commented Sep 19, 2015

OK got it, sorry, the docs for http://emberjs.com/api/classes/Ember.run.html#method_scheduleOnce are correct and the anonymous function case is dealt with, unfortunately I was only looking at the doc for the .once function without checking scheduleOnce: http://emberjs.com/api/classes/Ember.run.html#method_once

So technically, the docs are correct.. Replacing method Function | String with method String in the .once function docs would have helped in my case but then I just realized that the method must properly work with a non-anonymous function.. Sorry for waste of time.

@Leooo Leooo changed the title Ember.run.once running several times when method is a function Ember.run.once running several times when method is an anonymous function Sep 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants