Thursday, 14 June 2018

021 BackboneJS History

History

History like a global router , it keeps  track of the history, matches the appropriate route, trigger callbacks  and enables the routing in the application.


Javascript handle history object by creating pushState().But older browsers  does not support pushState . BackboneJS smartly handle new and old browser.you can handle enable / diasbale pushState by pushState: true/false .

After routers is created, and all of the routes are set up properly
, Backbone.history.start() to begin monitoring change.



Syntax : Backbone.history.start([options])

Example


<!DOCTYPE html>
<head>
<title>Router Extend Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>
</head>
<script type="text/javascript">
var MyRoute = Backbone.Router.extend({
routes: {
"Item": "myItem"
},
myItem: function (myroute) {
document.write(myroute);
}
});
var rtObj = new MyRoute();
Backbone.history.start({ pushState: true });
</script>
<body>
<a href="#route1">Route1 </a><a href="#route2">Route2 </a><a href="#route3">Route3
</a>
</body>

Wednesday, 13 June 2018

020 BackboneJS View extend,initialize,$el,Attributes

BackboneJS View

The Backbone.js view is how your data looks like.View represent model and collcetion to the user.BackboneJS  organize your interface into logical views, backed by models, each will updated independently when model changes and collection changes without having to redraw the page.View handle users input events, bind events with methods.


Following of methods that can be used to manipulate the Backbone.js views.



extend :
It is used to creating a custom view class. You can to override existing the render function.

Syntax : Backbone.View.extend(properties, [classProperties])


Example



<!DOCTYPE html>
<head>
<title>Router Extend Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>
<script type="text/javascript">

var Route1 = Backbone.View.extend({

initialize: function () {
document.write('I am learing BackboneJS');
}
});

var app = new Route1();


</script>

</head>


Output :

I am learing BackboneJS




constructor / initialize : Constructor is called when the view instance created."new" keyword is used for instance creation. 

Syntax : new View([options])

Example



<!DOCTYPE html>
<head>
<title>Router Extend Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>
<script type="text/javascript">

var Route1 = Backbone.View.extend({

initialize: function () {
document.write('I am learing BackboneJS');
}
});

var app = new Route1();


</script>

</head>


Output :

I am learing BackboneJS




el :
Any views have a DOM element .BackboneJS el method defines the element that is used as the view reference.el created from the view's tagName, className, id and attributes properties, if specified.


Syntax : view.el



Example



<!DOCTYPE html>
<head>
<title>Router Extend Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>
<script type="text/javascript">

var Route1 = Backbone.View.extend({

initialize: function () {
document.write('I am learing BackboneJS');
}
});

var app = new Route1({ el: $("myapp") });


</script>
<div id="myapp"></div>

</head>




Output :

I am learing BackboneJS 



$el :
$el method specifies a cached jQuery object for a view. 

Syntax : view.$el()


Attributes :
There are hash attributes that will be set as HTML DOM element attributes on the view el(id, class, data-properties).

Syntax : view.attributes

Example


<!DOCTYPE html>
<head>
<title>Router Extend Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>
<script type="text/javascript">

student = Backbone.Model.extend
({
defaults:
{
name: "",
class: ""
}
});


var ViewDemo = Backbone.View.extend ({
initialize: function ()
{
document.write("The site is: ",this.model.get("name")+"
"
);
document.write("The site is: ",this.model.get("class")+"
"
);
}

});

var StuObj = new student({ name: "John", class: "12" });

var myview = new ViewDemo({ model: StuObj });


</script>
</head>


Output :

The site is: John
The site is: 12









 










 




019 BackboneJS Backbone.Router extend,initialize,navigate,execute

Router 

Web page generally contain linkable, bookmarkable, shareable URLs for important locations.Router methods work for routing client-side pages and connecting them to actions and events.Backbone.Router also enabled history capabilities.You need to call Backbone.history.start() or Backbone.history.start({pushState: true}) after the application has finished creating router.




The following is the lists of methods which can be used for Router.
 
extend : It is used to extend the backbone router class.It helps to
create a custom router class.
Backbone.Router actually create a new constructor that inherits from the Backbone.Router.

Syntax : Backbone.Router.extend(properties, [classProperties])

Example


<script type="text/javascript">
 
var Router = Backbone.Router.extend({
                routes: {
                                //Your custom logig
                              },
               execute: function (callback, args)
                            {
                                //Your custom logig
                            }

                    }); 
 
Backbone.history.start();

</script>

routes : Routes method define URL representation of application objects on routers.
Syntax : router.routes


constructor / initialize :It creates a new constructor for the router object instantiation. 

Syntex : new Router([options])


Example


<script type="text/javascript">

//Rounter extend
var Router = Backbone.Router.extend({

                         routes: {

                                       //Your custom logig

                                    },

                              execute: function (callback, args) 
                 {

                                    //Your custom logig

                            }


});

//instance creation
var appRouter=new Router(); 
 
Backbone.history.start();


</script>





route :
create a route for the router.

Syntax : router.route(route, name, [callback])

Example of extend/constructor/routes



<!DOCTYPE html>
<head>
<title>Router Extend Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>
<script type="text/javascript">
var Router = Backbone.Router.extend({
routes: {
'': 'Router1',
'Router2': 'Router2'
},
Router1: function(){
document.write("Your Custom logic no 1 called.");
},
Router2: function(){
document.write("Your Custom logic no 2 called.");
},
});
var appRouter=new Router();
Backbone.history.start();


</script>
</head>
<body>


Output :
Your Custom logic no 1 called.  






navigate :
If we want to save our application as a URL, we call navigate to update. 

Syntax : router.navigate(fragment, [options])

Example



<!DOCTYPE html>
<head>
<title>Router Extend Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>
<script type="text/javascript">
var App = Backbone.Router.extend({

routes: {
"": "index",
"item": "GoSearch"
},

index: function () {
document.write('You are in the home page.');
},
GoSearch: function () {
app.navigate("www.google.com", { trigger: true });
}
});

app = new App();
Backbone.history.start();


</script>
</head>


Output :
You are in the home page.





execute :
router.execute is called internally within the router, its corresponding callback is executed whenever a route is matched.

Syntax : router.execute(callback, args)

Example




<!DOCTYPE html>
<head>
<title>Router Extend Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>
<script type="text/javascript">

var Route1 = Backbone.View.extend({


template: 'This is route 1',

initialize: function () {
this.execute();
},
execute: function () {
return this.template;
}
});

var App = Backbone.Router.extend({

routes: {
"": "index",
"item": "GoSearch"
},

index: function () {

document.write('You are in the home page.');


},
GoSearch: function () {
app.navigate("www.google.com", { trigger: true });

var route1 = new Route1();
alert(route1.execute());
}
});

app = new App();
Backbone.history.start();


</script>

<div id = "navigation">
<a href = "#item">route1</a>
</div>
<div id = "content></div>
</head>


Output :





বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...