20. January 2014

BackboneJS, view and its events

Working in BackboneJS is fun and easy to begin with. Slowly but steadily it gets complicated as we move on and add more to the project. Being said that, BackboneJS is not at all opinionated. So there is no rightway of doing something. If your solution works for your project, then its good to go. Well, then there are certain things, which may help you, better organise your code. One of such things is View Events. In a Backbone view, to listen to events and handle them, the general syntax is as this.

events:{
        "click": "onClick"
        },
onClick: function(event){
    //Do something with the event
    //or
    //specifically do it for a target
    var btnID = event.target.id;
    if(btnID==='btn_one'){
        console.log('TODO:');
    }
}

Well, it may be easier at first, but we are listening for events for the whole view and then in the handler checking it. There is a way in BackboneJS that directly and listens for events for that particular target. The code looks as this.

events:{
        "click #btn_one": "onClick"
        },
onClick: function(event){
    //btn_one is clicked
    //Do something with the event
}

Happy coding.

15. January 2014

Distribute iOS app on your web site

Its not that difficult as it sounds.
First of all, there are distribution services out there. I am not going to name them here, but they are available. A simple web search will show you that. Now, the question is, how do they do it, or the more significant question is, can we do it ourselves. The answer is yes. There are 3 basic things required for distribution.

  • .ipa file ( iOS app )
  • .plist file ( manifest file for the app )
  • .html file ( html file for showing the link to the user )

The iOS app must be compiled with the Ad-HOC distribution certificate with the device(where the app will be installed) UDIDs included. Thats the same procedure, as one would follow, while distributing through other stores. Once its compiled, you say Archive for distribution in XCode, which will create the .ipa file. The manifest file defines the application meta data, if you go through the manifest file below, there is a place to point to the .ipa file, make sure, you provide full URL to .ipa file. The download link on HTML file will point to this manifest file. Here is the boilerplate for a typical manifest file (which is a .plist file). The HTML file, is simply having a link pointing to the .plist file.

Now simply link to the plist file in the HTML file as below.

 <a href="itms-services://?action=download-manifest&url=https://myServer/iOS-dist/iOSAppDist.plist" class="button">Install My App</a>

Now visit the URL through iPhone, where your HTML file is stored. Once you click the link, it will download and install the iOS app.

Happy hacking.

14. January 2014

HTML5, requestAnimationFrame

Making a javascript application, at somepoint, involves use of setTimeout and setInterval functions. Well, the possibility increases even more, when one needs to animate elements. So we know what it means to use these two functions.
Fortunately we have a new method, called requestAnimationFrame(), which is elegant and effective. Now animations will not eat up mobile battery life, but still be smooth. This is optimised to takecare of the host environment, such as mobile browser, desktop browser etc. It even minimises the repeat call, if the application using it, is not visible or scrolled out of view, sweet.
Check, which browsers and versions support this function, just to make yourself clear.
Here are few links which explain requestAnimationFrame function in more detail.

Its kind of saying bye bye to setTimeout and setInterval. Happy Animating.