I am using backbone.js (0.5.3) with JQueryMobile (1.0 beta 2). I know there are routing conflicts when using those libraries together, and I would like to know if there is a solution to use them :
- without hacking into the jquery mobile source code like described in Ben Nolan's pots : http://bennolan.com/2010/11/23/backbone-and-jquery-mobile.html
- and without using another router like jquerymobile-router (unless there is no other option...)
My problem is quite similar to the one described in this post : jquery-mobile backbone.js routing
When when i make a request, the backbone render
code of the corresponding backbone view gets triggered before the new jquery page is fully loaded. I'm trying to render my html generated code in the $(".ui-page-active")
DOM element to target the page that is generated by jQueryMobile (or the page that is "activated") :
MyView = Backbone.View.extend({
el: $(".ui-page-active")
render: function(){
console.log(el)
}
});
But the el
attribute is empty when the render method is called, because jquery mobile has not yet rendered the dom...
Thanks for any help !
Update
Addy Osmani seems to have the answer to my question :) but it will be for the next part of his (great) tutorial : http://msdn.microsoft.com/en-us/scriptjunkie/hh377172.aspx
This may be a bit of a longshot as I have no way to test it, but try extending Backbone's history, and have it listen for a creation event before actually firing off the code. So..
That might do it, or at least get you on the right path I hope.
Ok the solution is to disable jQuery Mobile ajax loading feature and call the
$.mobile.changePage
method manually.HTML page :
Then whenever a new route is triggered, I first build my new "jQuery page canvas" in the Backbone View constructor, append it to the HTML document
body
and set myel
view element to this newdiv
:Backbone.View
And in the
render
method :Works like a charm and without any unnecessary hacks :)
Here is my full Backbone View if it can help anyone :
With jquery 1.2.0, disable ajax and linkBinding
after, which with normal Backbone routes, you can link a #id with