Adds BaseModel and option setters.
authordsc <dsc@less.ly>
Mon, 27 Feb 2012 21:43:35 +0000 (13:43 -0800)
committerdsc <dsc@less.ly>
Mon, 27 Feb 2012 21:43:35 +0000 (13:43 -0800)
lib/base.co [new file with mode: 0644]
lib/baseview.co [deleted file]
lib/graph/model.co
lib/scaffold/model.co

diff --git a/lib/base.co b/lib/base.co
new file mode 100644 (file)
index 0000000..a7d23c9
--- /dev/null
@@ -0,0 +1,111 @@
+_  = require 'kraken/underscore'
+op = require 'kraken/util/op'
+
+
+BaseModel = exports.BaseModel = Backbone.Model.extend do # {{{
+    ctorName : 'BaseModel'
+    # List of methods to bind on initialize; set on subclass
+    __bind__ : []
+    
+    idAttribute    : 'id'
+    valueAttribute : 'value'
+    
+    
+    initialize: ->
+        _.bindAll this, ...@__bind__ if @__bind__.length
+        @__super__ = @constructor.__super__
+    
+    
+    toKVPairs: (kv_delimiter='=') ->
+        idAttr  = @idAttribute or 'id'
+        key     = @[idAttr] or @get idAttr
+        valAttr = @valueAttribute or 'value'
+        value   = @get 'value'
+        if key and value?
+            "#{encodeURIComponent key}#kv_delimiter#{encodeURIComponent value}"
+        else
+            ''
+    
+    toString: -> "#{@ctorName}(id=#{@id}, value=#{@get 'value'})"
+
+
+# Class Methods
+BaseModel import do
+    
+    fromKVPairs: (o) ->
+        o   = _.fromKVPairs o if typeof o is 'string'
+        Cls = if typeof this is 'function' then this else this.constructor
+        new Cls o
+
+# }}}
+
+
+/**
+ * @class Base View, used by scaffold and others.
+ */
+BaseView = exports.BaseView = Backbone.View.extend do # {{{
+    ctorName : 'BaseView'
+    
+    # List of methods to bind on initialize; set on subclass
+    __bind__ : []
+    
+    
+    
+    initialize: ->
+        _.bindAll this, ...@__bind__ if @__bind__.length
+        @__super__ = @constructor.__super__
+        
+        @model.view = this
+        @$el.data { @model, view:this }
+        @model.on 'change',  @render, this
+        @model.on 'destroy', @remove, this
+    
+    toTemplateLocals: ->
+        json = {value:v} = @model.toJSON()
+        if _.isArray(v) or _.isObject(v)
+            json.value = JSON.stringify v
+        { $, _, op, @model, view:this } import json
+    
+    $template: (locals={}) ->
+        $ @template @toTemplateLocals() import locals
+    
+    build: ->
+        return this unless @template
+        
+        outer = @$template()
+        @$el.html outer.html()
+            .attr do
+                id    : outer.attr 'id'
+                class : outer.attr('class')
+        
+        this
+    
+    render: ->
+        @build()
+        @trigger 'render', this
+        this
+    
+    hide   : -> @$el.hide();      this
+    show   : -> @$el.show();      this
+    remove : -> @$el.remove();    this
+    clear  : -> @model.destroy(); @remove()
+    
+    
+    # remove : ->
+    #     if (p = @$el.parent()).length
+    #         @$parent or= p
+    #         # @parent_index = p.children().indexOf @$el
+    #     @$el.remove()
+    #     this
+    # 
+    # reparent : (parent=@$parent) ->
+    #     parent = $ parent
+    #     @$el.appendTo parent if parent?.length
+    #     this
+    
+    toString : -> "#{@ctorName}(model=#{@model})"
+
+
+# }}}
+
+
diff --git a/lib/baseview.co b/lib/baseview.co
deleted file mode 100644 (file)
index 40701ed..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-_  = require 'kraken/underscore'
-op = require 'kraken/util/op'
-
-
-BaseView = exports.BaseView = Backbone.View.extend do # {{{
-    # List of methods to bind on initialize; set on subclass
-    __bind__ : []
-    
-    
-    initialize: ->
-        _.bindAll this, ...@__bind__ if @__bind__.length
-        @__super__ = @constructor.__super__
-        
-        @model.view = this
-        @$el.data { @model, view:this }
-        @model.on 'change',  @render, this
-        @model.on 'destroy', @remove, this
-    
-    toTemplateLocals: ->
-        json = {value:v} = @model.toJSON()
-        if _.isArray(v) or _.isObject(v)
-            json.value = JSON.stringify v
-        { $, _, op, @model, view:this } import json
-    
-    $template: (locals={}) ->
-        $ @template @toTemplateLocals() import locals
-    
-    build: ->
-        return this unless @template
-        
-        outer = @$template()
-        @$el.html outer.html()
-            .attr do
-                id    : outer.attr 'id'
-                class : outer.attr('class')
-        
-        
-        this
-    
-    render: ->
-        @build()
-        @trigger 'render', this
-        this
-    
-    hide   : -> @$el.hide();      this
-    show   : -> @$el.show();      this
-    remove : -> @$el.remove();    this
-    clear  : -> @model.destroy(); @remove()
-    
-    
-    # remove : ->
-    #     if (p = @$el.parent()).length
-    #         @$parent or= p
-    #         # @parent_index = p.children().indexOf @$el
-    #     @$el.remove()
-    #     this
-    # 
-    # reparent : (parent=@$parent) ->
-    #     parent = $ parent
-    #     @$el.appendTo parent if parent?.length
-    #     this
-    
-    toString : -> "#{@ctorName}(model=#{@model})"
-
-
-# }}}
-
-
index 405c47e..c635414 100644 (file)
@@ -1,5 +1,6 @@
 _ = require 'kraken/underscore'
-{BaseView} = require 'kraken/baseview'
+{ BaseModel, BaseView,
+} = require 'kraken/base'
 { Field, FieldList, FieldView, Scaffold
 } = require 'kraken/scaffold'
 
@@ -28,7 +29,7 @@ class exports.TagSet extends Array
         indices   = ( for tag of tags then @get tag )
         if is_single then indices[0] else indices
     
-    toString: -> "Tags(length=#{@length}, values=[\"#{@join '", "'}\"])"
+    toString: -> "TagSet(length=#{@length}, values=[\"#{@join '", "'}\"])"
 
 
 KNOWN_TAGS = exports.KNOWN_TAGS = new TagSet()
@@ -122,7 +123,7 @@ GraphOptionList = exports.GraphOptionList = FieldList.extend do # {{{
  * Represents a Graph, including its charting options, dataset, annotations, and all
  * other settings for both its content and presentation.
  */
-GraphModel = exports.GraphModel = Backbone.Model.extend do # {{{
+GraphModel = exports.GraphModel = BaseModel.extend do # {{{
     ctorName : 'GraphModel'
     urlRoot  : '/graphs'
     
@@ -138,6 +139,29 @@ GraphModel = exports.GraphModel = Backbone.Model.extend do # {{{
             options : {}
         }
     
+    
+    hasOption: (key) ->
+        options = @get 'options', {}
+        key in options
+    
+    setOption: (key, value, opts) ->
+        options = @get 'options', {}
+        options[key] = value
+        @set 'options', options, opts
+    
+    getOption: (key, def) ->
+        options = @get 'options', {}
+        if key in options then options[key] else def
+    
+    unsetOption: (key, opts) ->
+        options = @get 'options', {}
+        delete options[key]
+        @set 'options', options, opts
+    
+    
+    toKVPairs: ->
+        ...
+    
     toString: -> "#{@ctorName}(id=#{@id}, name=#{@get 'name'}, dataset=#{@get 'dataset'})"
 # }}}
 
index 825995e..083c0e3 100644 (file)
@@ -1,13 +1,15 @@
 _    = require 'kraken/underscore'
 op   = require 'kraken/util/op'
+{BaseModel} = require 'kraken/base'
 
 
 
 ### Scaffold Models
 
-Field = exports.Field = Backbone.Model.extend do # {{{
-    ctorName    : 'Field'
-    idAttribute : 'name'
+Field = exports.Field = BaseModel.extend do # {{{
+    ctorName       : 'Field'
+    idAttribute    : 'name'
+    valueAttribute : 'value'
     
     
     initialize: ->
@@ -99,13 +101,6 @@ Field = exports.Field = Backbone.Model.extend do # {{{
         {id:@id} import do
             _.clone(@attributes) import { value:@getValue(), def:@get('default') }
     
-    toKVPairs: ->
-        key   = @get 'name'
-        value = @get 'value'
-        if value?
-            "#{encodeURIComponent key}=#{encodeURIComponent value}"
-        else
-            ''
     
     toString: -> "(#{@id}: #{@get 'value'})"
 # }}}