CodesJava

Easy learning with example program codes

Backbone.JS URL() collection

The Backbone.JS URL() collection method creates an instance of the collection. It returns where the resource is located.

Syntax:

Collection.URL 

Example:

<!DOCTYPE html>  
<html>
<head>  
<title>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>  
<body>  
<script type="text/javascript">  
var X = Backbone.Model.extend({});   
var Y = Backbone.Collection.extend({  
model: X   
});  
var site = Backbone.Model.extend({  
defaults: {  
user: null,  
page: []  
},  
initialize: function () {  
var Z = this;    
this.X = new X(this.get('user'));  
this.posts = new Y(this.get('page'));  
this.posts.url = function () {  
return Z.url() + '/page';  
};  },   
urlRoot: '/site/'  
});  
var attributes = {  
id: 12345,  
page:[{id: 54321}]  
}   
variab = new site(attributes);  
variab.posts.each(function (X) {  
document.write("URL: ",X.url());  
});  
</script>  
</body>  
</html>

Output:

URL: /site/12345/page/54321

Explanation:
In the above example, the URL() method displays the URL for the “site” model.

Please follow and like us:
error









Copyright © 2019 CodesJava DMCA.com Protection Status