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