瀏覽代碼

add init function to views

Benoît Hubert 8 年之前
父節點
當前提交
380b38dd17
共有 3 個文件被更改,包括 29 次插入9 次删除
  1. 3 2
      js/req-promise.js
  2. 17 5
      js/ws-forms.js
  3. 9 2
      js/ws-ui-parts.js

+ 3 - 2
js/req-promise.js

@@ -34,8 +34,9 @@
         success: function(data) {
           resolve(data);
         },
-        error: function(jqXHR) {
-          reject(new Error(jqXHR.responseText));
+        error: function(jqXHR, textStatus, errorThrown) {
+          console.log('this should throw a bloody error', jqXHR.responseText);
+          reject(new Error(errorThrown));
         }
       });
       $.ajax(options);

+ 17 - 5
js/ws-forms.js

@@ -47,15 +47,17 @@
 
   _ws.makeView('add-repo-form', {
 
+    init: function() {
+      this.$inputTitle = this.$elem.find('input[name="title"]');
+    },
+
     render: function() {
       console.log('render form add repo', this);
       this.$elem.addClass('in');
       this.$btn.removeClass('in');
-      this.$elem.find('input[name="title"]').focus();
+      this.$inputTitle.focus();
     },
 
-    $btn: $('#add-repo-btn'),
-
     events: {
       'click .icon-cross': function() {
         this.$elem.removeClass('in');
@@ -64,9 +66,19 @@
 
       'submit': function(e) {
         e.preventDefault();
-        console.log('submit form');
+        var title = this.$inputTitle.val();
+        rp.post('/collections', { title: title })
+        .then(function(c) {
+          _ws.notify('success', 'Collection créée: ' + c.title);
+        })
+        .catch(function(err) {
+          console.error(err);
+          _ws.notify('error', 'Impossible de créer la collection: ' + err.message);
+        });
       } 
-    }
+    },
+
+    $btn: $('#add-repo-btn')
 
   });
 

+ 9 - 2
js/ws-ui-parts.js

@@ -91,7 +91,7 @@
       _ws.ui[v.partName] = v;
 
       // Bind the provided render function
-      if(v.props.render) {
+      if(typeof v.props.render === 'function') {
         v._render = props.render.bind(v);
         v.render = viewRenderWrapper.bind(v);
       }
@@ -113,7 +113,7 @@
 
       // Pass arbitrary properties
       for(var p in props) {
-        if(['render', 'events'].indexOf(p) !== -1) {
+        if(['render', 'events', 'init'].indexOf(p) !== -1) {
           // console.log('pass props: ignore', p);
           continue;
         }
@@ -123,6 +123,13 @@
 
       v.show = viewElemShow.bind(v);
       v.hide = hideElemShow.bind(v);
+
+      // Init if an init function was provided
+      if(typeof v.props.init === 'function') {
+        var initFunc = v.props.init.bind(v);
+        initFunc();
+      }
+
     };