I want to use jQuery in my web application on CatberryJS framework. How can I do it?
我想在CatberryJS框架的web应用程序中使用jQuery。我该怎么做呢?
1 个解决方案
#1
4
Using jQuery library registered globally
You can use jQuery as usual, just include this script tag into your "head" component:
您可以像往常一样使用jQuery,只是将这个脚本标记添加到“head”组件中:
<script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
And then feel free to use jQuery functions in any component's code except its constructor and the "render" method.
然后您可以在任何组件的代码中使用jQuery函数,除了它的构造函数和“呈现”方法。
Using jQuery as a part of built bundle.js
In this case you can install jQuery using npm like this:
在这种情况下,您可以使用npm安装jQuery,如下所示:
npm install jquery --save
Then register jQuery as a Catberry Service in ./browser.js
然后在./ browsr .js中注册jQuery作为一个Catberry服务
var catberry = require('catberry'),
cat = catberry.create({/* config */}),
jQuery = require('jquery');
cat.locator.registerInstance('jquery', jQuery);
After the jQuery was registered, you can use it in your components resolving it from a Catberry Service Locator:
jQuery注册后,您可以在组件中使用它从Catberry服务定位器解析它:
var $ = this.$context.locator.resolve('jquery');
$('.class').html('<div>Hello, World!</div>');
Additionally
If you would like to use jQuery functions in component's constructor or component's "render" method you should check the environment flag like this:
如果您想在组件的构造函数或组件的“呈现”方法中使用jQuery函数,请检查以下环境标志:
if (this.$context.isBrowser) {
// crazy jQuery stuff
}
Also, keep in mind that to avoid memory leaks your component should unsubscribe all jQuery events and free all resources used by jQuery functions in "unbind" method.
此外,请记住,为了避免内存泄漏,组件应该取消订阅所有jQuery事件,并释放jQuery函数在“unbind”方法中使用的所有资源。
You can use any browser-side library inside component like it's described above for jQuery.
您可以在组件内部使用任何浏览器端库,就像上面对jQuery所描述的那样。
#1
4
Using jQuery library registered globally
You can use jQuery as usual, just include this script tag into your "head" component:
您可以像往常一样使用jQuery,只是将这个脚本标记添加到“head”组件中:
<script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
And then feel free to use jQuery functions in any component's code except its constructor and the "render" method.
然后您可以在任何组件的代码中使用jQuery函数,除了它的构造函数和“呈现”方法。
Using jQuery as a part of built bundle.js
In this case you can install jQuery using npm like this:
在这种情况下,您可以使用npm安装jQuery,如下所示:
npm install jquery --save
Then register jQuery as a Catberry Service in ./browser.js
然后在./ browsr .js中注册jQuery作为一个Catberry服务
var catberry = require('catberry'),
cat = catberry.create({/* config */}),
jQuery = require('jquery');
cat.locator.registerInstance('jquery', jQuery);
After the jQuery was registered, you can use it in your components resolving it from a Catberry Service Locator:
jQuery注册后,您可以在组件中使用它从Catberry服务定位器解析它:
var $ = this.$context.locator.resolve('jquery');
$('.class').html('<div>Hello, World!</div>');
Additionally
If you would like to use jQuery functions in component's constructor or component's "render" method you should check the environment flag like this:
如果您想在组件的构造函数或组件的“呈现”方法中使用jQuery函数,请检查以下环境标志:
if (this.$context.isBrowser) {
// crazy jQuery stuff
}
Also, keep in mind that to avoid memory leaks your component should unsubscribe all jQuery events and free all resources used by jQuery functions in "unbind" method.
此外,请记住,为了避免内存泄漏,组件应该取消订阅所有jQuery事件,并释放jQuery函数在“unbind”方法中使用的所有资源。
You can use any browser-side library inside component like it's described above for jQuery.
您可以在组件内部使用任何浏览器端库,就像上面对jQuery所描述的那样。