Skip to content
This repository
tree: 39ba4e0703
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 102 lines (85 sloc) 2.587 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

<style>
  body {
    font: 14px "Helvetica Neue", Helvetica, arial, sans-serif;
    padding: 60px;
  }
  p {
    margin: 5px;
  }
  input[type='text'] {
    padding: 5px 10px;
    border-width: 1px;
    -webkit-border-radius: 2px;
  }
</style>

<script src="build.js"></script>

<script>
   var domify = require('component-domify');

   // TODO: remove all this, real example..

   function errorHTML(Model) {
     Model.prototype.errorHTML = function(){
       return '<ul class=errors>\n' + this.errors.map(function(err){
         return ' <li>' + err.attr + ' ' + err.message + '</li>';
       }).join('\n') + '\n</ul>';
     };
   }

  function formHTML(Model) {
    Model.prototype.form = function(){
      var model = this.model;
      var attrs = model.attrs;
      var save = this.isNew() ? 'Save' : 'Update';
      var url = this.isNew() ? model.url() : this.url();
      var html = '';
      var attr;

      html += '<form action="' + url + '">\n';

      for (var name in attrs) {
        attr = attrs[name];
        if (attr.primaryKey) continue;
        if (false === attr.show) continue;
        html += ' <p>\n';
        html += ' <input type="text" placeholder="' + name + '" name="' + name + '" />\n';
        html += ' </p>\n\n';
      }

      html += ' <div class=actions>\n';
      html += ' <input type="submit" value="' + save + '"></input>\n';
      html += ' </div>\n';
      html += '</form>\n';

      return html;
    };
  }

  function required(attr) {
    return function(Model){
      Model.validate(function(model){
        if (!model.has(attr)) model.error(attr, 'field required');
      });
    }
  }

  function lengthAbove(attr, len) {
    return function(Model){
      Model.validate(function(model){
        if (!model.has(attr)) return;
        var str = model.get(attr);
        if (str.length <= len) model.error(attr, 'must be longer than ' + len + ' characters');
      });
    }
  }

  var model = require('model');

  var Post = model('Post')
    .attr('id', { type: 'number' })
    .attr('title', { type: 'string' })
    .attr('body', { type: 'string' })
    .attr('create_at', { type: 'date', show: false })
    .attr('update_at', { type: 'date', show: false })
    .use(errorHTML)
    .use(formHTML)
    .use(required('title'))
    .use(required('body'))
    .use(lengthAbove('title', 3))
    .use(lengthAbove('body', 20))

  var post = new Post({ title: 'Something', body: 'Some other thing here' });

  post.save(function(err){
    Post.get(post.id(), function(err, post){
      console.log(post);
    })
  });
  
</script>
Something went wrong with that request. Please try again.