Commit 193752e8 authored by Peter Müller's avatar Peter Müller Committed by Sindre Sorhus

Close GH-847: Ember: Prevent global leakage of 'use strict' directive.

parent 64af0c87
/*global Ember*/ /*global Ember*/
/*global DS*/ /*global DS*/
'use strict'; (function () {
'use strict';
DS.LSAdapter = DS.Adapter.extend(Ember.Evented, { DS.LSAdapter = DS.Adapter.extend(Ember.Evented, {
init: function () { init: function () {
this._loadData(); this._loadData();
}, },
generateIdForRecord: function () { generateIdForRecord: function () {
return Math.random().toString(32).slice(2).substr(0, 5); return Math.random().toString(32).slice(2).substr(0, 5);
}, },
find: function (store, type, id) { find: function (store, type, id) {
var namespace = this._namespaceForType(type); var namespace = this._namespaceForType(type);
return Ember.RSVP.resolve(Ember.copy(namespace.records[id])); return Ember.RSVP.resolve(Ember.copy(namespace.records[id]));
}, },
findMany: function (store, type, ids) { findMany: function (store, type, ids) {
var namespace = this._namespaceForType(type); var namespace = this._namespaceForType(type);
var results = []; var results = [];
for (var i = 0; i < ids.length; i++) { for (var i = 0; i < ids.length; i++) {
results.push(Ember.copy(namespace.records[ids[i]])); results.push(Ember.copy(namespace.records[ids[i]]));
} }
return Ember.RSVP.resolve(results); return Ember.RSVP.resolve(results);
}, },
// Supports queries that look like this: // Supports queries that look like this:
// //
// { // {
// <property to query>: <value or regex (for strings) to match>, // <property to query>: <value or regex (for strings) to match>,
// ... // ...
// } // }
// //
// Every property added to the query is an "AND" query, not "OR" // Every property added to the query is an "AND" query, not "OR"
// //
// Example: // Example:
// //
// match records with "complete: true" and the name "foo" or "bar" // match records with "complete: true" and the name "foo" or "bar"
// //
// { complete: true, name: /foo|bar/ } // { complete: true, name: /foo|bar/ }
findQuery: function (store, type, query, recordArray) { findQuery: function (store, type, query, recordArray) {
var namespace = this._namespaceForType(type); var namespace = this._namespaceForType(type);
var results = this.query(namespace.records, query); var results = this.query(namespace.records, query);
return Ember.RSVP.resolve(results); return Ember.RSVP.resolve(results);
}, },
query: function (records, query) { query: function (records, query) {
var results = []; var results = [];
var id, record, property, test, push; var id, record, property, test, push;
for (id in records) { for (id in records) {
record = records[id]; record = records[id];
for (property in query) { for (property in query) {
test = query[property]; test = query[property];
push = false; push = false;
if (Object.prototype.toString.call(test) === '[object RegExp]') { if (Object.prototype.toString.call(test) === '[object RegExp]') {
push = test.test(record[property]); push = test.test(record[property]);
} else { } else {
push = record[property] === test; push = record[property] === test;
}
}
if (push) {
results.push(record);
} }
} }
if (push) { return results;
results.push(record); },
findAll: function (store, type) {
var namespace = this._namespaceForType(type);
var results = [];
for (var id in namespace.records) {
results.push(Ember.copy(namespace.records[id]));
} }
return Ember.RSVP.resolve(results);
},
createRecord: function (store, type, record) {
var namespace = this._namespaceForType(type);
this._addRecordToNamespace(namespace, record);
this._saveData();
return Ember.RSVP.resolve();
},
updateRecord: function (store, type, record) {
var namespace = this._namespaceForType(type);
var id = record.get('id');
namespace.records[id] = record.toJSON({ includeId: true });
this._saveData();
return Ember.RSVP.resolve();
},
deleteRecord: function (store, type, record) {
var namespace = this._namespaceForType(type);
var id = record.get('id');
delete namespace.records[id];
this._saveData();
return Ember.RSVP.resolve();
},
// private
_getNamespace: function () {
return this.namespace || 'DS.LSAdapter';
},
_loadData: function () {
var storage = localStorage.getItem(this._getNamespace());
this._data = storage ? JSON.parse(storage) : {};
},
_saveData: function () {
localStorage.setItem(this._getNamespace(), JSON.stringify(this._data));
},
_namespaceForType: function (type) {
var namespace = type.url || type.toString();
return this._data[namespace] || (
this._data[namespace] = {records: {}}
);
},
_addRecordToNamespace: function (namespace, record) {
var data = record.serialize({includeId: true});
namespace.records[data.id] = data;
} }
return results; });
}, })();
findAll: function (store, type) {
var namespace = this._namespaceForType(type);
var results = [];
for (var id in namespace.records) {
results.push(Ember.copy(namespace.records[id]));
}
return Ember.RSVP.resolve(results);
},
createRecord: function (store, type, record) {
var namespace = this._namespaceForType(type);
this._addRecordToNamespace(namespace, record);
this._saveData();
return Ember.RSVP.resolve();
},
updateRecord: function (store, type, record) {
var namespace = this._namespaceForType(type);
var id = record.get('id');
namespace.records[id] = record.toJSON({ includeId: true });
this._saveData();
return Ember.RSVP.resolve();
},
deleteRecord: function (store, type, record) {
var namespace = this._namespaceForType(type);
var id = record.get('id');
delete namespace.records[id];
this._saveData();
return Ember.RSVP.resolve();
},
// private
_getNamespace: function () {
return this.namespace || 'DS.LSAdapter';
},
_loadData: function () {
var storage = localStorage.getItem(this._getNamespace());
this._data = storage ? JSON.parse(storage) : {};
},
_saveData: function () {
localStorage.setItem(this._getNamespace(), JSON.stringify(this._data));
},
_namespaceForType: function (type) {
var namespace = type.url || type.toString();
return this._data[namespace] || (
this._data[namespace] = {records: {}}
);
},
_addRecordToNamespace: function (namespace, record) {
var data = record.serialize({includeId: true});
namespace.records[data.id] = data;
}
});
/*global Todos, Ember */ /*global Todos, Ember */
'use strict'; (function () {
'use strict';
Todos.TodoController = Ember.ObjectController.extend({ Todos.TodoController = Ember.ObjectController.extend({
isEditing: false, isEditing: false,
// We use the bufferedTitle to store the original value of // We use the bufferedTitle to store the original value of
// the model's title so that we can roll it back later in the // the model's title so that we can roll it back later in the
// `cancelEditing` action. // `cancelEditing` action.
bufferedTitle: Ember.computed.oneWay('title'), bufferedTitle: Ember.computed.oneWay('title'),
actions: { actions: {
editTodo: function () { editTodo: function () {
this.set('isEditing', true); this.set('isEditing', true);
}, },
doneEditing: function () { doneEditing: function () {
var bufferedTitle = this.get('bufferedTitle').trim(); var bufferedTitle = this.get('bufferedTitle').trim();
if (Ember.isEmpty(bufferedTitle)) { if (Ember.isEmpty(bufferedTitle)) {
// The `doneEditing` action gets sent twice when the user hits // The `doneEditing` action gets sent twice when the user hits
// enter (once via 'insert-newline' and once via 'focus-out'). // enter (once via 'insert-newline' and once via 'focus-out').
// //
// We debounce our call to 'removeTodo' so that it only gets // We debounce our call to 'removeTodo' so that it only gets
// made once. // made once.
Ember.run.debounce(this, 'removeTodo', 0); Ember.run.debounce(this, 'removeTodo', 0);
} else { } else {
var todo = this.get('model'); var todo = this.get('model');
todo.set('title', bufferedTitle); todo.set('title', bufferedTitle);
todo.save(); todo.save();
}
// Re-set our newly edited title to persist its trimmed version
this.set('bufferedTitle', bufferedTitle);
this.set('isEditing', false);
},
cancelEditing: function () {
this.set('bufferedTitle', this.get('title'));
this.set('isEditing', false);
},
removeTodo: function () {
this.removeTodo();
} }
// Re-set our newly edited title to persist its trimmed version
this.set('bufferedTitle', bufferedTitle);
this.set('isEditing', false);
},
cancelEditing: function () {
this.set('bufferedTitle', this.get('title'));
this.set('isEditing', false);
}, },
removeTodo: function () { removeTodo: function () {
this.removeTodo(); var todo = this.get('model');
}
},
removeTodo: function () { todo.deleteRecord();
var todo = this.get('model'); todo.save();
},
todo.deleteRecord();
todo.save();
},
saveWhenCompleted: function () { saveWhenCompleted: function () {
this.get('model').save(); this.get('model').save();
}.observes('isCompleted') }.observes('isCompleted')
}); });
})();
/*global Todos, Ember */ /*global Todos, Ember */
'use strict'; (function () {
'use strict';
Todos.TodosController = Ember.ArrayController.extend({
actions: { Todos.TodosController = Ember.ArrayController.extend({
createTodo: function () { actions: {
var title, todo; createTodo: function () {
var title, todo;
// Get the todo title set by the "New Todo" text field
title = this.get('newTitle').trim(); // Get the todo title set by the "New Todo" text field
if (!title) { title = this.get('newTitle').trim();
return; if (!title) {
} return;
}
// Create the new Todo model
todo = this.store.createRecord('todo', { // Create the new Todo model
title: title, todo = this.store.createRecord('todo', {
isCompleted: false title: title,
}); isCompleted: false
todo.save(); });
todo.save();
// Clear the "New Todo" text field
this.set('newTitle', ''); // Clear the "New Todo" text field
}, this.set('newTitle', '');
},
clearCompleted: function () {
var completed = this.get('completed'); clearCompleted: function () {
completed.invoke('deleteRecord'); var completed = this.get('completed');
completed.invoke('save'); completed.invoke('deleteRecord');
completed.invoke('save');
},
}, },
},
/* properties */ /* properties */
remaining: Ember.computed.filterBy('content', 'isCompleted', false), remaining: Ember.computed.filterBy('content', 'isCompleted', false),
completed: Ember.computed.filterBy('content', 'isCompleted', true), completed: Ember.computed.filterBy('content', 'isCompleted', true),
allAreDone: function (key, value) { allAreDone: function (key, value) {
if (value !== undefined) { if (value !== undefined) {
this.setEach('isCompleted', value); this.setEach('isCompleted', value);
return value; return value;
} else { } else {
var length = this.get('length'); var length = this.get('length');
var completedLength = this.get('completed.length'); var completedLength = this.get('completed.length');
return length > 0 && length === completedLength; return length > 0 && length === completedLength;
} }
}.property('length', 'completed.length') }.property('length', 'completed.length')
}); });
})();
/*global Todos, Ember */ /*global Todos, Ember */
'use strict'; (function () {
'use strict';
Ember.Handlebars.helper('pluralize', function (singular, count) { Ember.Handlebars.helper('pluralize', function (singular, count) {
/* From Ember-Data */ /* From Ember-Data */
var inflector = new Ember.Inflector(Ember.Inflector.defaultRules); var inflector = new Ember.Inflector(Ember.Inflector.defaultRules);
return count === 1 ? singular : inflector.pluralize(singular); return count === 1 ? singular : inflector.pluralize(singular);
}); });
})();
/*global Todos, DS */ /*global Todos, DS */
'use strict'; (function () {
'use strict';
Todos.Todo = DS.Model.extend({ Todos.Todo = DS.Model.extend({
title: DS.attr('string'), title: DS.attr('string'),
isCompleted: DS.attr('boolean') isCompleted: DS.attr('boolean')
}); });
})();
/*global Ember, Todos */ /*global Ember, Todos */
'use strict'; (function () {
'use strict';
Todos.Router.map(function () { Todos.Router.map(function () {
this.resource('todos', { path: '/' }, function () { this.resource('todos', { path: '/' }, function () {
this.route('active'); this.route('active');
this.route('completed'); this.route('completed');
});
}); });
});
Todos.TodosRoute = Ember.Route.extend({ Todos.TodosRoute = Ember.Route.extend({
model: function () { model: function () {
return this.store.find('todo'); return this.store.find('todo');
} }
}); });
Todos.TodosIndexRoute = Ember.Route.extend({ Todos.TodosIndexRoute = Ember.Route.extend({
setupController: function () { setupController: function () {
this.controllerFor('todos').set('filteredTodos', this.modelFor('todos')); this.controllerFor('todos').set('filteredTodos', this.modelFor('todos'));
} }
}); });
Todos.TodosActiveRoute = Ember.Route.extend({ Todos.TodosActiveRoute = Ember.Route.extend({
setupController: function () { setupController: function () {
var todos = this.store.filter('todo', function (todo) { var todos = this.store.filter('todo', function (todo) {
return !todo.get('isCompleted'); return !todo.get('isCompleted');
}); });
this.controllerFor('todos').set('filteredTodos', todos); this.controllerFor('todos').set('filteredTodos', todos);
} }
}); });
Todos.TodosCompletedRoute = Ember.Route.extend({ Todos.TodosCompletedRoute = Ember.Route.extend({
setupController: function () { setupController: function () {
var todos = this.store.filter('todo', function (todo) { var todos = this.store.filter('todo', function (todo) {
return todo.get('isCompleted'); return todo.get('isCompleted');
}); });
this.controllerFor('todos').set('filteredTodos', todos); this.controllerFor('todos').set('filteredTodos', todos);
} }
}); });
})();
/*global Todos, Ember */ /*global Todos, Ember */
'use strict'; (function () {
'use strict';
Todos.EditTodoView = Ember.TextField.extend({ Todos.EditTodoView = Ember.TextField.extend({
focusOnInsert: function () { focusOnInsert: function () {
// Re-set input value to get rid of a reduntant text selection // Re-set input value to get rid of a reduntant text selection
this.$().val(this.$().val()); this.$().val(this.$().val());
this.$().focus(); this.$().focus();
}.on('didInsertElement') }.on('didInsertElement')
}); });
Ember.Handlebars.helper('edit-todo', Todos.EditTodoView); Ember.Handlebars.helper('edit-todo', Todos.EditTodoView);
})();
/*global Todos, Ember */ /*global Todos, Ember */
'use strict'; (function () {
'use strict';
Todos.TodosView = Ember.View.extend({ Todos.TodosView = Ember.View.extend({
focusInput: function () { focusInput: function () {
this.$('#new-todo').focus(); this.$('#new-todo').focus();
}.on('didInsertElement') }.on('didInsertElement')
}); });
})();
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment