Adds .getClassName() to BaseMixin
authordsc <dsc@wikimedia.org>
Thu, 12 Apr 2012 22:49:43 +0000 (15:49 -0700)
committerdsc <dsc@wikimedia.org>
Thu, 12 Apr 2012 22:49:43 +0000 (15:49 -0700)
lib/base/base-mixin.co
lib/base/base-model.co
lib/base/base-view.co

index 00d25ce..93385b5 100644 (file)
@@ -7,6 +7,7 @@ Backbone = require 'backbone'
 
 BaseBackboneMixin = exports.BaseBackboneMixin =
     
+    
     initialize: ->
         @__apply_bind__()
     
@@ -75,6 +76,12 @@ BaseBackboneMixin = exports.BaseBackboneMixin =
             self.unwait(); fn ...
     
     
+    getClassName: ->
+        "#{@..name or @..displayName}"
+    
+    toString: ->
+        "#{@getClassName()}()"
+
 
 mixinBase = exports.mixinBase = (body) ->
     _.clone(BaseBackboneMixin) import body
index 5eb7c18..229f29e 100644 (file)
@@ -70,12 +70,16 @@ BaseModel = exports.BaseModel = Backbone.Model.extend mixinBase do # {{{
      * Like `.toJSON()` in that it should return a plain object with no functions,
      * but for the purpose of `.toKV()`, allowing you to customize the values
      * included and keys used.
+     * 
+     * @param {Object} [opts={}] Options:
+     * @param {Boolean} [opts.keepFunctions=false] If false, functions will be omitted from the result.
      * @returns {Object}
      */
-    toKVPairs: ->
+    toKVPairs: (opts={}) ->
+        opts = {-keepFunctions, ...opts}
         kvo = _.collapseObject @toJSON()
         for k, v in kvo
-            kvo[k] = @serialize v
+            kvo[k] = @serialize v if opts.keepFunctions or typeof v is not 'function'
         kvo
     
     /**
@@ -92,7 +96,8 @@ BaseModel = exports.BaseModel = Backbone.Model.extend mixinBase do # {{{
     toURL: ->
         "?#{@toKV ...}"
     
-    toString: -> "#{@..name or @..displayName}(cid=#{@cid}, id=#{@id})"
+    toString: ->
+        "#{@getClassName()}(cid=#{@cid}, id=#{@id})"
 
 
 # Class Methods
@@ -138,7 +143,11 @@ BaseList = exports.BaseList = Backbone.Collection.extend mixinBase do # {{{
     toURL: (item_delim='&', kv_delim='=') ->
         "?#{@toKV ...}"
     
-    toString: -> "#{@..name or @..displayName}(length=#{@length})"
+    toString: ->
+        modelIds = @models
+            .map -> "\"#{it.id ? it.cid}\""
+            .join ', '
+        "#{@getClassName()}[#{@length}](#modelIds)"
 # }}}
 
 
index 9c4e5ec..2378094 100644 (file)
@@ -137,7 +137,8 @@ BaseView = exports.BaseView = Backbone.View.extend mixinBase do # {{{
     #     @$el.appendTo parent if parent?.length
     #     this
     
-    toString : -> "#{@..name or @..displayName}(model=#{@model})"
+    toString : ->
+        "#{@getClassName()}(model=#{@model})"
 
 
 # Proxy model methods