JSFiddle - React, Tailwind, and code Playground
HTML
<script src="//canjs.com/release/2.1.3/can.jquery.js"></script>
<div id="out"></div>
<script id="app" type="text/stache">
<router page="todosList" page-id=""></router>
</script>
<script id="router" type="text/stache">
{{#if isTodosList}}
<todos-list></todos-list>
{{/if}}
</script>
<script id="todos-list" type="text/stache">
<input type="text" name="tagFilter" placeholder="Tag lookup" can-enter="tagFiltered" />
<input type="text" name="todo" placeholder="What needs to be done" can-enter="todoCreated" />
{{#each tagFiltered}}
{{name}}<div class="button">Details</div><div class="button" can-click="destroy">Done</div>
{{/each}}
</script>
JavaScript
var todos = [
// todos
{id: 1, name: "Get the thing working", detail: "Nibh invenire convenire et cum", tag: "stuff"},
{id: 2, name: "Get out", detail: "Nibh invenire convenire et cum", tag: "stuff"},
{id: 3, name: "Drink and eat", detail: "Nibh invenire convenire et cum", tag: "otherstuff"}
];
var start = (function(can, $, out, todos) {
var Todo = can.Model.extend({
// todo model
// fetches things and so on
findAll: function() {
// get all todos
return $.Deferred().resolve(todos);
},
findOne: function(params) {
// get one todo
return $.Deferred().resolve(todos[(+params.id) - 1]);
},
create: function(attributes) {
// creates new todo
var last = todos[todos.length - 1];
$.extend(attributes, {id: last.id + 1, detail: "", tag: ""});
todos.push(attributes);
return $.Deferred().resolve(attributes);
},
update: function(id, attributes) {
// update one todo
$.extend(todos[id - 1], attributes);
return $.Deferred().resolve();
},
destroy: function() {
// destroy todo
return $.Deferred().resolve();
}
}, {});
var TodoList = Todo.List.extend({
filter: function(check) {
// filter todos
var list = [];
this.each(function(todo) {
if(check(todo)) list.push(todo);
});
return list;
}
});
can.Component.extend({
// router component
// handles page routing
tag: "router",
template: can.view("router"),
scope: {
page: "@",
isTodosList: function() {
// are we seeing todos list
return this.attr("page") === "todosList";
}
}
});
TodosListViewModel = can.Map.extend({
todoCreated: function(context, element) {
// new todo is created
var Todo = this.Todo;
new Todo({
name: can.trim(element.val())
}).save();
element.val("");
},
tagFiltered: function(context, element) {
// filter...