Skip to content

Wrapper for using requireJS as JSLoader#260

Merged
ocombe merged 1 commit intoocombe:masterfrom
tokafew420:master
Nov 2, 2015
Merged

Wrapper for using requireJS as JSLoader#260
ocombe merged 1 commit intoocombe:masterfrom
tokafew420:master

Conversation

@tokafew420
Copy link
Contributor

jsLoader callback param expects err as the first argument, but require()
has separate callbacks for success and error. Create a wrapper to
resolve conflict. Also add jsLoader.requirejs property for loaders.core
to determine filetype and errors
fixes #178

jsLoader callback param expects err as the first argument, but require()
has separate callbacks for success and error. Create a wrapper to
resolve conflict. Also add jsLoader.requirejs property for loaders.core
to determine filetype and errors
ocombe added a commit that referenced this pull request Nov 2, 2015
Wrapper for using  requireJS as JSLoader
@ocombe ocombe merged commit dc4a48e into ocombe:master Nov 2, 2015
@ocombe
Copy link
Owner

ocombe commented Nov 2, 2015

Hmm it looks like the requirejs example isn't working anymore with this merged :-/

@ocombe
Copy link
Owner

ocombe commented Nov 2, 2015

OK I fixed it, not sure why there was a bind(null, null), but it works without it.

@tokafew420
Copy link
Contributor Author

Sorry this is my fault, the callback should be bound to like so, callback.bind(null,undefined).
Binding the first argument to null causes the error checking to return a false positive because of the angular.isDefined:

if (angular.isDefined(err) && ($delegate.jsLoader.hasOwnProperty("ocLazyLoadLoader") || $delegate.jsLoader.hasOwnProperty("requirejs")))

The "callback.bind(null, undefined)" is so that the callback passed to jsLoader() is called correctly by requirejs . The Nodejs callback convention is callback(err, vaue, ..)but requirejs expects 2 separate callbacks, one for defined values callback(value, ...) and another for error callback(err). Without the binding, when require-ing a define() module, the callback will be called with the return of the define() and the promise (jsDefered) is rejected.

Also, if may be better to not use angular.isDefined(err) in the case that null is passed as the error, as is usually done with nodejs callbacks. Perhaps something like this:

if (err && ($delegate.jsLoader.hasOwnProperty("ocLazyLoadLoader") || $delegate.jsLoader.hasOwnProperty("requirejs")))

@ocombe
Copy link
Owner

ocombe commented Nov 2, 2015

You're right :)
Any chance to get a new PR with this fix ?

@tokafew420
Copy link
Contributor Author

I submitted PR #264 for the success callback binding.

I thought about updating the callback error checking mentioned above thinking that angularjs has isNullOrUndefined() [or something similar] but there isn't. I didn't want to litter the source with angular.isDefine() && err !== null as this seems better suited for a utility function.

ocombe added a commit that referenced this pull request Nov 24, 2015
fix: success callback for requirejs wrapper

Fixes #260
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

Successfully merging this pull request may close these issues.

jsLoader incorrect signature when using requirejs

2 participants