1

I have followed this really good tutorial here

http://www.appliness.com/getting-started-with-html-mobile-application-development-using-jquery-mobile-requirejs-and-backbonejs/#codesyntax_9

Which is a starter point for jQuery Mobile and Backbone.

I understand that due to backbone and jQuery-Mobile both having built in routing services that jQuery-Mobile has theirs switched off.

One of the main reasons why I wanted to use such a package (as well as the UI styling) was for the page transitions (i.e pop or flip) but when I added these in to the index page of the attached example they did nothing (I am guessing that is due to the fact that something is disabled).

Does anyone know a way round this and if my diagnostic correct?

Thanks

Jack
  • 10,943
  • 13
  • 50
  • 65
Dan
  • 1,295
  • 2
  • 22
  • 46

3 Answers3

2

I'm not sure where you saw that jQuery-mobile has it's "routing" switched off, but it is true that both jQuery-mobile and Backbone.js work with the hash tag, and there are some issues with using both at the same time.

What you might want to look at is the jQuery-mobile-router plugin which was made specifically for this purpose (that is using jQuery-mobile together with backbone.js), in addition it has support for the jQuery-mobile page events.

You might also want to take a look at the following SO posts that are related to using backbone.js and jQuery-mobile together.

Backbone.js and jQueryMobile routing without hack or other router

jquery-mobile backbone.js routing

Community
  • 1
  • 1
Jack
  • 10,943
  • 13
  • 50
  • 65
  • Thanks Jack, sorry I meant that in the example I attached they had disabled the jQueryMobile ajax navigation system.... I will have a look at the other jQuery-mobile-router plugin.... guess i wanted to use backbones routing and still have the page transitions available – Dan Nov 14 '12 at 03:14
  • I'm not sure if it's worth *hacking* it to use the two of them together, remember that one of the advantages of backbone.js is that you can use whatever parts of it suit you (that is it isn't an all or nothing proposition) so you can use the rest of backbone.js and just use the plugin for routing. – Jack Nov 14 '12 at 03:18
  • @Dan Glad to help. You can mark an anwser as accepted by clicking the check-mark to the left of it. – Jack Nov 14 '12 at 04:44
  • I will need to get 15 reputation first – Dan Nov 14 '12 at 10:23
  • You only need [15 rep](http://stackoverflow.com/faq#reputation) to up-vote, to [accept an anwser](http://stackoverflow.com/faq#howtoask) you should see an outline of a check-mark to the left of the anwser below the arrows. – Jack Nov 14 '12 at 11:56
  • @Dan I don't think so, after you click the check-mark the outline gets filled in green... – Jack Nov 14 '12 at 13:27
1

The way to fix this is described in this tutorial: http://andidog.de/blog/2012/06/using-jquery-mobile-with-backbone-how-to-solve-routing-conflicts-and-use-mvc-for-the-application/

In router.js, change this line:

$.mobile.changePage($(view.el), {changeHash:false});

to this:

$.mobile.changePage($(view.el), {transition: 'slide', changeHash:false});

Or in jqm-config.js you can just add a default page transition like this:

$.mobile.defaultPageTransition = "slide";

Now, this will always use the forward slide (right to left) transition. If you want it to slide the opposite way when going back, you can do something like this. This is what's working for me. This SO answer helped me figure it out: https://stackoverflow.com/a/11660991/1665818. In router.js add these variables:

previousFragments: [],
backDetected: false,

And change the changePage function:

changePage:function (view) {
    //add the attribute 'data-role="page" ' for each view's div
    view.$el.attr('data-role', 'page');

    var currentFragment = Backbone.history.getFragment();
    this.backDetected = false;

    if(!window.linkClicked && currentFragment == this.previousFragments[this.previousFragments.length-2]) {
        this.backDetected = true;
        this.previousFragments.pop();
    } else {
        this.previousFragments.push(currentFragment);
    }
    //reset
    window.linkClicked = false;
    console.log("this.backDetected= "+this.backDetected);

    //append to dom
    $('body').append(view.$el);

    if(!this.init){
        $.mobile.changePage($(view.el), {reverse: this.backDetected, changeHash:false});
    }else{
        this.init = false;
    }
}

Then in main.js add a window.linkClicked variable at the top:

window.linkClicked = false;

And add an event listener inside the document ready function:

$(document).ready(function() {
  console.log("DOM IS READY");// Handler for .ready() called.

  $(document).on('click', 'a', function() {
    window.linkClicked = true;
  });

});
Community
  • 1
  • 1
aharris88
  • 3,560
  • 3
  • 27
  • 45
0

I was running in similar problems. Please find my solution below

Step 1) By default $.mobile.linkBindingEnabled will be true, so you can comment if there is any line in your code which is like this "$.mobile.linkBindingEnabled = false;".

Step 2) Use data-ajax=false in the hyperlinks which you don't want jquery mobile to bother. So by using this we can make use of Backbone.js Routers and also page transition effects of jQuery mobile in the app.

Example

Edit Page

You would have used this "#edit" in your Backbone.js router and don't want jQuery to interfere, so this here this data-ajax will do our need.

I have used the above technique and successful with that.