Created
June 2, 2013 08:36
-
-
Save ahomu/5693041 to your computer and use it in GitHub Desktop.
backbone sample http://jsbin.com/anigap/1/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> | |
<script src="http://documentcloud.github.io/underscore/underscore-min.js"></script> | |
<script src="http://documentcloud.github.io/backbone/backbone-min.js"></script> | |
<meta charset=utf-8 /> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<section id="js-view"> | |
</section> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Bookmarks= Backbone.Collection.extend({ | |
url: 'http://b.hatena.ne.jp/entrylist/json?sort=count&url=http://havelog.ayumusato.com' | |
}); | |
var List = Backbone.View.extend({ | |
initialize: function() { | |
_.bindAll(this); | |
this.collection.fetch({ | |
dataType: 'jsonp', | |
success: this.render | |
}); | |
}, | |
render: function() { | |
this.collection.each(this.addItemView); | |
return this; | |
}, | |
addItemView: function(model) { | |
var item = new Item({model: model}); | |
this.$el.append(item.render().el); | |
} | |
}); | |
var Item = Backbone.View.extend({ | |
events: { | |
'click a': 'confirm' | |
}, | |
confirm: function(evt) { | |
return confirm(evt.currentTarget.getAttribute('href') + 'をひらく?'); | |
}, | |
render: function() { | |
this.$el.html( | |
'<div>' + | |
'<a href="' + this.model.get('link') + '">'+ | |
this.model.get('title') + '(' + this.model.get('count') + ')' + | |
'</a>' + | |
'</div>' | |
); | |
return this; | |
} | |
}); | |
new List({ | |
el: '#js-view', | |
collection: new Bookmarks() | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment