CanJS jQuery Template
Includes jQuery, CanJS and all of its plugins. Use it as a starting point when you want to demo something.
by cherif_b
HTML
<script src="http://canjs.us/release/latest/can.jquery.js"></script>
<script src="http://canjs.us/release/latest/can.construct.proxy.js"></script>
<script src="http://canjs.us/release/latest/can.control.plugin.js"></script>
<script src="http://canjs.us/release/latest/can.observe.attributes.js"></script>
<script src="http://canjs.us/release/latest/can.observe.backup.js"></script>
<script src="http://canjs.us/release/latest/can.observe.delegate.js"></script>
<script src="http://canjs.us/release/latest/can.observe.setter.js"></script>
<script src="http://canjs.us/release/latest/can.observe.validations.js"></script>
<script src="http://canjs.us/release/latest/can.view.modifiers.js"></script>
<script src="http://canjs.us/release/latest/can.fixture.js"></script>
<script src="http://canjs.us/release/latest/can.view.mustache.js"></script>
<!-- YOUR CODE HERE -->
<div id="dummy"></div>
<!-- PUT ANY TEMPLATES YOU NEED HERE -->
<script type="text/mustache" id="posts">
<ul>
{{#posts}}
<li class="post" {{data 'post'}}><a href="#">{{title}}</a></li>
{{/posts}}
</ul>
</script>
CSS
.header {
background: #E1E3E1;
padding: 10px 0;
border-bottom: 1px solid #B5B6B5;
margin-bottom: 15px;
}
h1 {
font-size: 24px;
font-weight: bold;
}
p {
margin: 10px 0;
}
JavaScript
var POSTS = [{
"id": "1",
"title": "Welcome!",
"content": "This blog system is developed using Yii. It is meant to demonstrate how to use Yii to build a complete real-world application. Complete source code may be found in the Yii releases.\n\nFeel free to try this system by writing new posts and posting comments.",
"tags": "yii, blog",
"status": "2",
"create_time": "1230952187",
"update_time": "1230952187",
"author_id": "1"
}, {
"id": "2",
"title": "A Test Post",
"content": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
"tags": "test",
"status": "2",
"create_time": "1230952187",
"update_time": "1230952187",
"author_id": "1"
}];
can.fixture("GET /posts.json", function () {
return POSTS;
});
can.fixture("GET /posts/{id}.json", function (id) {
return POSTS[id];
});
var Post = can.Model({
findAll: "GET /posts.json",
findOne: "GET /posts/{id}.json"
}, {});
var Posts = can.Control({
'init': function (element, options) {
var self = this;
can.view('posts', {
posts: Post.findAll({})
}).then(function (frag) {
self.element.html(frag);
});
},
'li.post click': function (el) {
var post = el.data('post');
alert(post.attr('title'));
}
});
new Posts('#dummy');