You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ro...@apache.org on 2014/09/08 15:24:16 UTC

[2/4] fauxton commit: updated refs/heads/master to bacf6fd

Config: refactor: move method to collection


Project: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/commit/0ad739cf
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/tree/0ad739cf
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/diff/0ad739cf

Branch: refs/heads/master
Commit: 0ad739cf9bf894ea12da39ea424b7fbc705fdc08
Parents: 0717396
Author: Robert Kowalski <ro...@kowalski.gd>
Authored: Fri Sep 5 19:03:09 2014 +0200
Committer: Robert Kowalski <ro...@kowalski.gd>
Committed: Mon Sep 8 15:25:41 2014 +0200

----------------------------------------------------------------------
 app/addons/config/resources.js        | 10 ++++++
 app/addons/config/tests/configSpec.js | 50 ++++++++++++++++++------------
 app/addons/config/views.js            | 26 +++++++---------
 3 files changed, 51 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/0ad739cf/app/addons/config/resources.js
----------------------------------------------------------------------
diff --git a/app/addons/config/resources.js b/app/addons/config/resources.js
index bbd0d2a..28087ed 100644
--- a/app/addons/config/resources.js
+++ b/app/addons/config/resources.js
@@ -52,16 +52,26 @@ function (app, FauxtonAPI) {
 
   Config.Collection = Backbone.Collection.extend({
     model: Config.Model,
+
     documentation: "config",
+
     comparator: function (OptionModel) {
       if (OptionModel.get("section")) {
         return OptionModel.get("section");
       }
     },
+
     url: function () {
       return app.host + '/_config';
     },
 
+    findEntryInSection: function (sectionName, entry) {
+      var section = _.findWhere(this.toJSON(), {"section": sectionName}),
+          options = _.findWhere(section.options, {name: entry});
+
+      return options;
+    },
+
     parse: function (resp) {
       return _.map(resp, function (section, section_name) {
         return {

http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/0ad739cf/app/addons/config/tests/configSpec.js
----------------------------------------------------------------------
diff --git a/app/addons/config/tests/configSpec.js b/app/addons/config/tests/configSpec.js
index a2b9ee8..67301de 100644
--- a/app/addons/config/tests/configSpec.js
+++ b/app/addons/config/tests/configSpec.js
@@ -16,28 +16,33 @@ define([
       'testUtils'
 ], function (FauxtonAPI, Resources, Views, testUtils) {
   var assert = testUtils.assert,
-      ViewSandbox = testUtils.ViewSandbox;
+      ViewSandbox = testUtils.ViewSandbox,
+      collection;
 
-  describe("Config: Modal", function () {
-    var optionModels = [],
-        viewSandbox,
-        modal,
-        collection;
 
-    beforeEach(function (done) {
-      _.each([1, 2, 3], function (i) {
-        optionModels
-          .push(new Resources.OptionModel({
-            section: "foo" + i,
-            name: "bar" + i,
-            options: [{
-              name: "testname"
-            }]
-          }));
+  beforeEach(function () {
+    var optionModels = [];
+
+    _.each([1, 2, 3], function (i) {
+      var model = new Resources.OptionModel({
+        section: "foo" + i,
+        name: "bar" + i,
+        options: [{
+          name: "testname"
+        }]
       });
 
-      collection = new Resources.Collection(optionModels);
+      optionModels.push(model);
+    });
 
+    collection = new Resources.Collection(optionModels);
+  });
+
+  describe("Config: Modal", function () {
+    var viewSandbox,
+        modal;
+
+    beforeEach(function (done) {
       modal = new Views.Modal({
         collection: collection
       });
@@ -53,14 +58,19 @@ define([
     it("looks if entries are new", function () {
       modal.$('input[name="section"]').val("foo1");
       modal.$('input[name="name"]').val("testname");
-      assert.ok(modal.isNew(collection));
+      assert.ok(modal.isUniqueEntryInSection(collection));
 
       modal.$('input[name="name"]').val("testname2");
-      assert.notOk(modal.isNew(collection));
+      assert.notOk(modal.isUniqueEntryInSection(collection));
     });
-
   });
 
+  describe("Config: Collection", function () {
+    it("looks if entries are new", function () {
+      assert.ok(collection.findEntryInSection("foo1", "testname"));
+      assert.notOk(collection.findEntryInSection("foo1", "testname2"));
+    });
+  });
 
   describe("Config: TableRow", function () {
     var tabMenu, optionModel;

http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/0ad739cf/app/addons/config/views.js
----------------------------------------------------------------------
diff --git a/app/addons/config/views.js b/app/addons/config/views.js
index 1725157..71b4839 100644
--- a/app/addons/config/views.js
+++ b/app/addons/config/views.js
@@ -44,12 +44,6 @@ function(app, FauxtonAPI, Config, Components) {
       this.remove();
     },
 
-    uniqueName: function(name){
-      var section = _.findWhere(this.collection.toJSON(), {"section":this.model.get("section")});
-
-      return _.findWhere(section.options, {name: name});
-    },
-
     editValue: function (event) {
       this.$(event.currentTarget).find(".js-show-value").addClass("js-hidden");
       this.$(event.currentTarget).find(".js-edit-value-form").removeClass("js-hidden");
@@ -81,12 +75,16 @@ function(app, FauxtonAPI, Config, Components) {
     },
     saveAndRender: function (event) {
       var options = {},
-          $input = this.$(event.currentTarget).parents('td').find(".js-value-input");
+          $input = this.$(event.currentTarget).parents('td').find(".js-value-input"),
+          sectionName,
+          nameInSectionExists;
 
       options[$input.attr('name')] = $input.val();
 
-      if ($input.attr('name')==='name'){
-        if (this.uniqueName($input.val())){
+      if ($input.attr('name') === 'name') {
+        sectionName = this.model.get("section");
+        nameInSectionExists = this.collection.findEntryInSection(sectionName, $input.val());
+        if (nameInSectionExists) {
           this.error = FauxtonAPI.addNotification({
             msg: "This config already exists, enter a unique name",
             type: "error",
@@ -195,13 +193,11 @@ function(app, FauxtonAPI, Config, Components) {
       Views.Events.trigger("newSection");
 
     },
-    isNew: function(collection){
+    isUniqueEntryInSection: function (collection) {
       var sectionName = this.$('input[name="section"]').val(),
-          name = this.$('input[name="name"]').val();
-          var section = _.findWhere(collection.toJSON(), {"section":sectionName});
-          var options = _.findWhere(section.options, {name: name});
+          entry = this.$('input[name="name"]').val();
 
-          return options;
+      return collection.findEntryInSection(sectionName, entry);
     },
     isSection: function(){
       var section = this.$('input[name="section"]').val();
@@ -220,7 +216,7 @@ function(app, FauxtonAPI, Config, Components) {
         this.errorMessage("Add a name");
       } else if (!value) {
         this.errorMessage("Add a value");
-      } else if (this.isNew(collection)){
+      } else if (this.isUniqueEntryInSection(collection)) {
         this.errorMessage("Must have a unique name");
       } else {
         this.submitForm();