Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

split up code from view of example1 messages module

  • Loading branch information...
commit c55f5d4237860e0b0e1377e76f587daab01fc59e 1 parent 942effe
Rasmus Andersson authored August 06, 2010
74  examples/example1/client/messages.html
... ...
@@ -1,77 +1,3 @@
1  
-<script type="text/javascript" charset="utf-8">
2  
-  oui.app.on('start', function(ev){
3  
-    // Add header
4  
-    var header = fragment('header');
5  
-    $('body').append(header);
6  
-
7  
-    // Enable creation form
8  
-    var createForm = header.find('form');
9  
-    createForm.submit(function(ev){
10  
-      var message = {}, fieldsToClearOnSuccess = [];
11  
-      ['title', 'message'].forEach(function(k){
12  
-        var field = createForm.find(':input[name='+k+']')[0];
13  
-        if (field) {
14  
-          message[k] = field.value;
15  
-          fieldsToClearOnSuccess.push(field);
16  
-        }
17  
-      });
18  
-      console.log('posting message', JSON.stringify(message));
19  
-      oui.http.POST('/messages', {message:message}, function(err, rsp) {
20  
-        if (err) return console.error(err);
21  
-        fieldsToClearOnSuccess.forEach(function(field){
22  
-          field.value = '';
23  
-        });
24  
-        reloadMessages();
25  
-      });
26  
-      return false;
27  
-    });
28  
-
29  
-    // Setup messages
30  
-    var messageList = fragment('list', {}/*, function(){
31  
-      // this is run each time the fragment is rendered (i.e. on update())
32  
-      this.find('a[href$=/delete]').click(function(ev){
33  
-        removeMessage((/\/(\d+)\/delete$/.exec(this.href))[1]);
34  
-        return false;
35  
-      });
36  
-    }*/);
37  
-    $('body').append(messageList);
38  
-    function reloadMessages(callback) {
39  
-      oui.http.GET('/messages', function(err, rsp) {
40  
-        var messages = (rsp && rsp.data) ? rsp.data : [];
41  
-        if (callback) callback(err, messages);
42  
-        if (err) return console.error(err);
43  
-        messages.sort(function(a,b){ return a.id > b.id ? -1 : 1; }); // id desc
44  
-        messageList.context.messages = messages;
45  
-        messageList.update();
46  
-        messageList.find('a[href$=/delete]').click(function(ev){
47  
-          removeMessage((/\/(\d+)\/delete$/.exec(this.href))[1]);
48  
-          return false;
49  
-        });
50  
-      });
51  
-    }
52  
-
53  
-    // Remove a message
54  
-    function removeMessage(id) {
55  
-      oui.http.GET('/messages/'+id+'/delete', function(err, rsp) {
56  
-        if (err) return console.error(err);
57  
-        console.log('removed message', id);
58  
-        reloadMessages();
59  
-      });
60  
-    }
61  
-
62  
-    // Load messages
63  
-    reloadMessages(function(){
64  
-      var $reloadButton = $('<input type="button" value="Reload">');
65  
-      $('body').append(
66  
-        $reloadButton.click(function(){
67  
-          $reloadButton.attr('disabled', 'disabled');
68  
-          reloadMessages(function(){ $reloadButton.removeAttr('disabled'); });
69  
-        })
70  
-      );
71  
-    });
72  
-  });
73  
-</script>
74  
-
75 1
 <style type="text/css" media="screen">
76 2
   ul.block {
77 3
     list-style: none;
71  examples/example1/client/messages.js
... ...
@@ -0,0 +1,71 @@
  1
+oui.app.on('start', function(ev){
  2
+  // Add header
  3
+  var header = fragment('header');
  4
+  $('body').append(header);
  5
+
  6
+  // Enable creation form
  7
+  var createForm = header.find('form');
  8
+  createForm.submit(function(ev){
  9
+    var message = {}, fieldsToClearOnSuccess = [];
  10
+    ['title', 'message'].forEach(function(k){
  11
+      var field = createForm.find(':input[name='+k+']')[0];
  12
+      if (field) {
  13
+        message[k] = field.value;
  14
+        fieldsToClearOnSuccess.push(field);
  15
+      }
  16
+    });
  17
+    console.log('posting message', JSON.stringify(message));
  18
+    oui.http.POST('/messages', {message:message}, function(err, rsp) {
  19
+      if (err) return console.error(err);
  20
+      fieldsToClearOnSuccess.forEach(function(field){
  21
+        field.value = '';
  22
+      });
  23
+      reloadMessages();
  24
+    });
  25
+    return false;
  26
+  });
  27
+
  28
+  // Setup messages
  29
+  var messageList = fragment('list', {}/*, function(){
  30
+    // this is run each time the fragment is rendered (i.e. on update())
  31
+    this.find('a[href$=/delete]').click(function(ev){
  32
+      removeMessage((/\/(\d+)\/delete$/.exec(this.href))[1]);
  33
+      return false;
  34
+    });
  35
+  }*/);
  36
+  $('body').append(messageList);
  37
+  function reloadMessages(callback) {
  38
+    oui.http.GET('/messages', function(err, rsp) {
  39
+      var messages = (rsp && rsp.data) ? rsp.data : [];
  40
+      if (callback) callback(err, messages);
  41
+      if (err) return console.error(err);
  42
+      messages.sort(function(a,b){ return a.id > b.id ? -1 : 1; }); // id desc
  43
+      messageList.context.messages = messages;
  44
+      messageList.update();
  45
+      messageList.find('a[href$=/delete]').click(function(ev){
  46
+        removeMessage((/\/(\d+)\/delete$/.exec(this.href))[1]);
  47
+        return false;
  48
+      });
  49
+    });
  50
+  }
  51
+
  52
+  // Remove a message
  53
+  function removeMessage(id) {
  54
+    oui.http.GET('/messages/'+id+'/delete', function(err, rsp) {
  55
+      if (err) return console.error(err);
  56
+      console.log('removed message', id);
  57
+      reloadMessages();
  58
+    });
  59
+  }
  60
+
  61
+  // Load messages
  62
+  reloadMessages(function(){
  63
+    var $reloadButton = $('<input type="button" value="Reload">');
  64
+    $('body').append(
  65
+      $reloadButton.click(function(){
  66
+        $reloadButton.attr('disabled', 'disabled');
  67
+        reloadMessages(function(){ $reloadButton.removeAttr('disabled'); });
  68
+      })
  69
+    );
  70
+  });
  71
+});

0 notes on commit c55f5d4

Please sign in to comment.
Something went wrong with that request. Please try again.