Last active
December 10, 2015 02:18
-
-
Save schovi/4366428 to your computer and use it in GitHub Desktop.
CanJS simple control example
This file contains 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
<div <%= (el) -> can.data(el,'article',this) %> class="hidden article article-<%= this.id %>" <%= (el) -> el.fadeIn()%> > | |
<%= this.name %> | |
</div> |
This file contains 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
<div class="articles"> | |
<% can.each(this.articles, function(i, article) { %> | |
<%== can.view.render('article.ejs', article) %> | |
<% } %> | |
</div> |
This file contains 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
ArticlesControl = can.Control({ | |
init: function() { | |
var self = this; | |
can.view('articles.mustache', { | |
articles: Article.findAll() | |
}).done(function(frag) { | |
self.element.html(frag) | |
}) | |
}, | |
".article click": function(el, ev) { | |
ev.preventDefault(); | |
can.data(el, 'article').destroy() | |
} | |
// Tricky thing with observers and events :) | |
"{Article} created": function(model, event, article) { | |
this.element.find('articles').append(can.view('article.ejs', article)) | |
}, | |
// Tricky thing with observers and events :) | |
"{Article} destroyed": function(model, event, article) { | |
// Now you are 100% sure that server respond with success | |
this.element.find('.article-' + article.id).fadeOut(function(el) { | |
el.remove() | |
}) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment