BreezeJS RequireJS support unstable (knockout example)

蹲街弑〆低调 提交于 2019-12-12 04:57:01

问题


As I'm trying to compile my project with breeze and requirejs for production, I'm running into multiple issues. Unrolling this, I tried starting fresh with the Knockout RequireJS example. After I installed MVS2013Pro I was able to run the project, but not without issues:

  1. window.breeze is defined so it has been leaked to global scope (this should not happen with RequireJS)

  2. Hitting F5 Refresh will crash with

    • Uncaught Error: Can't find breeze and/or Q (breeze savequeuing)
    • Uncaught TypeError: undefined is not a function (breeze dataservice)

With these errors I'm not even trying to establish my grunt requirejs (r optimizer) task. As these are the root of my problems.

Note: I'm not searching for a knockout solution, my stack actually is: MongoDataService+Breeze+SaveQueueing+RequireJS+AngularJS+Socket.IO+Cordova


回答1:


window.breeze

You're right that breeze sneaks back into the browser way down at the bottom of breeze.debug.js:

if (global.window) {
    global.window.breeze = breeze;
}

We don't know why we put that in the code base. No one remembers. Coming at the end as it does, it clearly isn't needed by Breeze itself.

I commented it out in preliminary testing and that seems to do the trick. The window.breeze becomes undefined in the Todo-KO-Require sample; it remains defined (properly) in samples that do not use require.js.

We don't think this would be a breaking change for anyone ... and the remedy is simple enough: define it yourself at the top of your app.

We'll probably remove these lines for the next release unless we discover a reason to do otherwise.

F5 fail?

I can't repro this at all. I suspect your are conflating your discovery that breeze is inadvertently in the global namespace with your efforts to write an Angular app that uses breeze.savequeuing.js.

Why do I think this? Because the error you describe arises when trying to use breeze.savequeuing.js in an Angular app which is explicitly disallowed in the comments (line #14) of v.1.0.4:

Depends on Breeze (which it patches) and Q.js (not for use in Angular ... yet)

You no doubt omitted Q.js from your required libraries and it all goes to custard from there.

I'm personally allergic to the "save-when-user-changes-anything" user experience that made save queuing necessary. I'm almost sorry I wrote it. But there it is.

The apparently dependence on Q.js is the reason it isn't available for Angular apps as written. It's actually pretty easy to make it Angular-ready now because we recently (quietly) made accessible the component that Breeze is using for promises

breeze.Q // returns the promises component currently used by Breeze

Therefore, you could change the one reference to Q on line #104 to

    var deferredSave = breeze.Q.defer();

This should work when using breeze v.1.4.14 and later. I haven't fully tested it yet.

This change (or something like it) likely will find its way into breeze.savequeuing.js v.1.1.0 (as I just pushed a non-release version of that).



来源:https://stackoverflow.com/questions/24468484/breezejs-requirejs-support-unstable-knockout-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!