Skip to content

Commit

Permalink
Merge pull request #578 from stephenplusplus/canjs_require
Browse files Browse the repository at this point in the history
restructured canjs_require + code style.
  • Loading branch information
stephenplusplus committed May 29, 2013
2 parents d59b261 + ac44ecc commit 27850ea
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 49 deletions.
62 changes: 34 additions & 28 deletions labs/dependency-examples/canjs_require/js/app.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
/*global require*/
/*global require */
require.config({
paths: {
jquery: '../bower_components/jquery/jquery',
can: '../bower_components/CanJS/amd/can'
}
});

require(['can/util/library', 'can/route', 'app/todos', 'app/models/todo', 'can/view/ejs', 'can/view/mustache'],
function (can, route, Todos, Model) {
'use strict';
require([
'can/util/library',
'can/route',
'controls/todos',
'models/todo',
'can/view/ejs',
'can/view/mustache'
], function (can, route, Todos, Model) {
'use strict';

// Set up a route that maps to the `filter` attribute
route(':filter');
// Delay routing until we initialized everything
route.ready(false);
// Set up a route that maps to the `filter` attribute
route(':filter');
// Delay routing until we initialized everything
route.ready(false);

// View helper for pluralizing strings
can.Mustache.registerHelper('todoPlural', function (str, attr) {
return str + (attr.call(this.todos) !== 1 ? 's' : '');
});
// View helper for pluralizing strings
can.Mustache.registerHelper('todoPlural', function (str, attr) {
return str + (attr.call(this.todos) !== 1 ? 's' : '');
});

// Find all Todos
Model.findAll({}, function (todos) {
// Wire it up. Instantiate a new Todos control
new Todos('#todoapp', {
// The (Todo) model that the control should use
Model: Model,
// The list of Todos retrieved from the model
todos: todos,
// The control state for filtering the view (in our case the router)
state: can.route,
// The view to render
view: 'views/todos.mustache'
});
// Find all Todos
Model.findAll({}, function (todos) {
// Wire it up. Instantiate a new Todos control
new Todos('#todoapp', {
// The (Todo) model that the control should use
Model: Model,
// The list of Todos retrieved from the model
todos: todos,
// The control state for filtering the view (in our case the router)
state: can.route,
// The view to render
view: 'views/todos.mustache'
});

// Now we can start routing
route.ready(true);
});

// Now we can start routing
route.ready(true);
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/*global define*/
define(['can/util/library', 'can/control'], function (can, Control) {
/*global define, window */
/*jshint newcap:false */
define([
'can/util/library',
'can/control'
], function (can, Control) {
'use strict';

var ENTER_KEY = 13;
Expand All @@ -26,8 +30,8 @@ define(['can/util/library', 'can/control'], function (can, Control) {
text: value,
complete: false
}).save(function () {
el.val('');
});
el.val('');
});
}
},

Expand Down Expand Up @@ -112,4 +116,4 @@ define(['can/util/library', 'can/control'], function (can, Control) {
});

return Todos;
});
});
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
/*global define*/
define(['can/util/library', 'can/model'], function (can, Model) {
/*global define, window */
/*jshint newcap:false */
define([
'can/util/library',
'can/model'
], function (can, Model) {
'use strict';

var LocalStorage = Model({
// Implement local storage handling
localStore: function (cb) {
var name = this.name,
data = JSON.parse(window.localStorage[name] || (window.localStorage[name] = '[]')),
res = cb.call(this, data);
var name = this.name;
var data = JSON.parse(window.localStorage[name] || (window.localStorage[name] = '[]'));
var res = cb.call(this, data);

if (res !== false) {
can.each(data, function (todo) {
delete todo.editing;
});

window.localStorage[name] = JSON.stringify(data);
}
},

findAll: function () {
var def = new can.Deferred();
this.localStore(function (todos) {
var instances = [],
self = this;
var instances = [];
var self = this;
can.each(todos, function (todo) {
instances.push(new self(todo));
});
Expand Down Expand Up @@ -54,7 +60,8 @@ define(['can/util/library', 'can/model'], function (can, Model) {
},

update: function (id, attrs) {
var def = new can.Deferred(), todo;
var def = new can.Deferred();
var todo;
this.localStore(function (todos) {
for (var i = 0; i < todos.length; i++) {
if (todos[i].id === id) {
Expand All @@ -70,5 +77,4 @@ define(['can/util/library', 'can/model'], function (can, Model) {
}, {});

return LocalStorage;

});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/*global define*/
define(['can/util/library', 'can/observe', 'app/models/localstorage'], function (can, Observe, LocalStorage) {
/*global define */
/*jshint newcap:false */
define([
'can/util/library',
'can/observe',
'models/localstorage'
], function (can, Observe, LocalStorage) {
'use strict';

// Basic Todo entry model
Expand Down Expand Up @@ -37,18 +42,18 @@ define(['can/util/library', 'can/observe', 'app/models/localstorage'], function

// Returns a new can.Observe.List that contains only the Todos
// matching the current filter
byFilter: function(filter) {
byFilter: function (filter) {
var filtered = new Observe.List();
can.each(this, function(todo) {
if(todo.matches(filter)) {
can.each(this, function (todo) {
if (todo.matches(filter)) {
filtered.push(todo);
}
});
return filtered;
},

// Returns the list to display based on the currently set `filter`
displayList: function() {
displayList: function () {
return this.byFilter(this.attr('filter'));
}
});
Expand Down

0 comments on commit 27850ea

Please sign in to comment.