Permalink
Browse files

Upgraded to Ember 1.0.0 with new actions object

  • Loading branch information...
1 parent 6bb0080 commit bac759597e8be34289198d512ad74ea400d75636 @Wildhoney committed Oct 2, 2013
Showing with 93 additions and 84 deletions.
  1. +82 −74 packages/ember-droplet/ember-droplet-mixin.js
  2. +2 −2 packages/ember-droplet/ember-droplet-view.js
  3. +9 −8 tests/spec.js
View
156 packages/ember-droplet/ember-droplet-mixin.js
@@ -38,85 +38,93 @@ window.DropletController = Ember.Mixin.create({
},
/**
- * @method addValidFile
- * @param file {File}
- * Adds a valid file to the collection.
- * @return {Object}
- */
- addValidFile: function(file) {
- return this._addFile(file, true);
- },
-
- /**
- * @method addInvalidFile
- * @param file {File}
- * Adds an invalid file to the collection.
- * @return {Object}
- */
- addInvalidFile: function(file) {
- return this._addFile(file, false);
- },
-
- /**
- * @method deleteFile
- * @param file
- * Deletes a file from the collection.
- * @return {Object}
- */
- deleteFile: function(file) {
- Ember.set(file, 'deleted', true);
- return file;
- },
-
- /**
- * @method clearAllFiles
- * Clears all of the files from the collection.
- * @return {void}
- */
- clearAllFiles: function() {
- Ember.set(this, 'files', []);
- },
-
- /**
- * @method uploadAllFiles
- * Uploads all of the files that haven't been uploaded yet, but are valid files.
- * @return {Object} jQuery promise.
+ * @property actions
+ * @type {Object}
*/
- uploadAllFiles: function() {
-
- // Find the URL, set the uploading status, and create our promise.
- var url = Ember.get(this, 'dropletUrl'),
- deferred = new jQuery.Deferred();
-
- Ember.set(this, 'uploadStatus.uploading', true);
- Ember.set(this, 'uploadStatus.error', false);
-
- // Assert that we have the URL specified in the controller that implements the mixin.
- Ember.assert('You must specify the `dropletUrl` parameter in order to upload files.', !!url);
-
- // Create a new XHR request object.
- var request = new XMLHttpRequest();
- request.open('post', url, true);
-
- // Create a new form data instance.
- var formData = new FormData();
+ actions: {
+
+ /**
+ * @method addValidFile
+ * @param file {File}
+ * Adds a valid file to the collection.
+ * @return {Object}
+ */
+ addValidFile: function(file) {
+ return this._addFile(file, true);
+ },
+
+ /**
+ * @method addInvalidFile
+ * @param file {File}
+ * Adds an invalid file to the collection.
+ * @return {Object}
+ */
+ addInvalidFile: function(file) {
+ return this._addFile(file, false);
+ },
+
+ /**
+ * @method deleteFile
+ * @param file
+ * Deletes a file from the collection.
+ * @return {Object}
+ */
+ deleteFile: function(file) {
+ Ember.set(file, 'deleted', true);
+ return file;
+ },
+
+ /**
+ * @method clearAllFiles
+ * Clears all of the files from the collection.
+ * @return {void}
+ */
+ clearAllFiles: function() {
+ Ember.set(this, 'files', []);
+ },
+
+ /**
+ * @method uploadAllFiles
+ * Uploads all of the files that haven't been uploaded yet, but are valid files.
+ * @return {Object} jQuery promise.
+ */
+ uploadAllFiles: function() {
+
+ // Find the URL, set the uploading status, and create our promise.
+ var url = Ember.get(this, 'dropletUrl'),
+ deferred = new jQuery.Deferred();
+
+ Ember.set(this, 'uploadStatus.uploading', true);
+ Ember.set(this, 'uploadStatus.error', false);
+
+ // Assert that we have the URL specified in the controller that implements the mixin.
+ Ember.assert('You must specify the `dropletUrl` parameter in order to upload files.', !!url);
+
+ // Create a new XHR request object.
+ var request = new XMLHttpRequest();
+ request.open('post', url, true);
+
+ // Create a new form data instance.
+ var formData = new FormData();
+
+ // Iterate over each file, and append it to the form data.
+ Ember.EnumerableUtils.forEach(Ember.get(this, 'validFiles'), function(file) {
+ formData.append('file', file.file);
+ }, this);
- // Iterate over each file, and append it to the form data.
- Ember.EnumerableUtils.forEach(Ember.get(this, 'validFiles'), function(file) {
- formData.append('file', file.file);
- }, this);
+ // Add all of the event listeners.
+ this._addProgressListener(request.upload);
+ this._addSuccessListener(request.upload, deferred);
+ this._addErrorListener(request.upload, deferred);
- // Add all of the event listeners.
- this._addProgressListener(request.upload);
- this._addSuccessListener(request.upload, deferred);
- this._addErrorListener(request.upload, deferred);
+ // Set the request size, and then we can upload the files!
+ request.setRequestHeader('X-File-Size', this._getSize());
+ request.send(formData);
- // Set the request size, and then we can upload the files!
- request.setRequestHeader('X-File-Size', this._getSize());
- request.send(formData);
+ // Return the promise.
+ return deferred.promise();
- // Return the promise.
- return deferred.promise();
+ }
},
View
4 packages/ember-droplet/ember-droplet-view.js
@@ -181,12 +181,12 @@ window.DropletView = Ember.View.extend({
// Determine if the file is valid based on its MIME type.
if ($.inArray(file.type, mimeTypes) === -1) {
// If it isn't valid, then we'll add it as an invalid file.
- controller.addInvalidFile(file);
+ controller.send('addInvalidFile', file);
continue;
}
// Otherwise the file has a valid MIME type, and therefore be added as a good file.
- controller.addValidFile(file);
+ controller.send('addValidFile', file);
}
View
17 tests/spec.js
@@ -21,7 +21,7 @@ describe('Ember Crossfilter', function() {
it('Can add a valid file to the list', function() {
var file = { name: 'MyFile.png' };
- controller.addValidFile(file);
+ controller.send('addValidFile', file);
expect(Ember.get(controller, 'validFiles.length')).toEqual(1);
expect(Ember.get(controller, 'invalidFiles.length')).toEqual(0);
expect(Ember.get(controller, 'uploadedFiles.length')).toEqual(0);
@@ -30,27 +30,28 @@ describe('Ember Crossfilter', function() {
it('Can add an invalid file to the list', function() {
var file = { name: 'MyFile.xml' };
- controller.addInvalidFile(file);
+ controller.send('addInvalidFile', file);
expect(Ember.get(controller, 'validFiles.length')).toEqual(0);
expect(Ember.get(controller, 'invalidFiles.length')).toEqual(1);
expect(Ember.get(controller, 'uploadedFiles.length')).toEqual(0);
expect(Ember.get(controller, 'deletedFiles.length')).toEqual(0);
});
it('Can delete a file from the list', function() {
- var file = controller.addValidFile({ name: 'MyFile.png' });
- controller.deleteFile(file);
+ controller.send('addValidFile', { name: 'MyFile.png' });
+ var file = controller.files[0];
+ controller.send('deleteFile', file);
expect(Ember.get(controller, 'validFiles.length')).toEqual(0);
expect(Ember.get(controller, 'invalidFiles.length')).toEqual(0);
expect(Ember.get(controller, 'uploadedFiles.length')).toEqual(0);
expect(Ember.get(controller, 'deletedFiles.length')).toEqual(1);
});
it('Can clear all files from the list', function() {
- controller.addValidFile({ name: 'MyFile.png' });
- controller.addValidFile({ name: 'AnotherFile.xml' });
- controller.addValidFile({ name: 'LoveLetter.txt' });
- controller.clearAllFiles();
+ controller.send('addValidFile', { name: 'MyFile.png' });
+ controller.send('addValidFile', { name: 'AnotherFile.xml' });
+ controller.send('addValidFile', { name: 'LoveLetter.txt' });
+ controller.send('clearAllFiles');
expect(Ember.get(controller, 'files.length')).toEqual(0);
expect(Ember.get(controller, 'validFiles.length')).toEqual(0);
expect(Ember.get(controller, 'invalidFiles.length')).toEqual(0);

0 comments on commit bac7595

Please sign in to comment.