Using jquery custom widgets with backbone.js views
-
12-06-2021 - |
문제
I have a custom jQuery widget that is being called from my app that uses backbone.js for MVC. How can I use the backbone events pattern in my custom jQuery widget?
function( $ ) {
$.widget( "medex.chooser", {
...
_create: function() {
// Would I create a new backbone view here???
}
}
Thanks.
해결책
I think you need to define a custom View that is then called by your custom widget.
In your app initialization code, for example:
APP = {}; // your app's global object
APP.Views = {};
APP.Views.WidgetView = Backbone.View.extend(
{
events: {
"click .grid1" : "onGrid1Click"
},
initialize: function() {
// code here
},
onGrid1Click : function(evt) {
// code here
}
});
Then this will be the constructor code for your widget:
function Widget(element) {
this.view = new APP.Views.WidgetView({ el: element });
}
This code might not be entirely valid, but should give you an idea of the structure you were looking for. Lemme know if this works for you.
다른 팁
If you're still looking for an answer, you could convert a widget into a view. The basic rules for converting from a Widget to a View are these:
- _create becomes initialize
- this.element becomes this.$el
- refresh becomes render
- destroy becomes remove
- $('.someDiv').myWidget() becomes new MyView({ el: $('.someDiv') });
- options have to become constructor parameters, but can be handled a couple of different ways
제휴하지 않습니다 StackOverflow