From 8d677a82b596134eda42fbf65a2bc6cbcf6a466d Mon Sep 17 00:00:00 2001 From: dsc Date: Wed, 7 Mar 2012 10:59:05 -0800 Subject: [PATCH] Supports slug parsing and slug URLs. --- docs/todo.md | 5 ----- lib/main.co | 22 +++++++++------------- lib/server/server.co | 2 +- lib/vis/vis-model.co | 14 +++++++++++++- lib/vis/vis-view.co | 2 +- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/docs/todo.md b/docs/todo.md index 135365f..0a54206 100644 --- a/docs/todo.md +++ b/docs/todo.md @@ -3,13 +3,8 @@ ## bugs -- defaults should only be filtered out for toURL() and toJSON() - redraw button! -- valueFormatter, axisLabelFormatter - (Safari) dygraph on safari has a bug parsing dates, bug http://code.google.com/p/dygraphs/issues/detail?id=287 -- (Chrome) When changing a checkbox, the graph doesn't seem to get updated. The model is updated though, and when a non-checkbox option is modified the checkbox-option is also updated in the view - - E.g. when setting showRoller=true , the input doesn't get displayed right away. Also, when resetting to showRoller=false it doesn't disappear -- huge issues coordinating subview insertion of HTML blocks -- need to add events and provide an abstraction ## ux diff --git a/lib/main.co b/lib/main.co index 70da9c1..1cf6af4 100644 --- a/lib/main.co +++ b/lib/main.co @@ -33,22 +33,18 @@ main = -> console.log 'StateChange!\n\n', String(root.location), '\n\n' - # If we got querystring args, apply them to the graph data = {} - if String(root.location).split '?' .1 + + # Process URL + loc = String root.location + + # If we got querystring args, apply them to the graph + if loc.split '?' .1 data = _.uncollapseObject _.fromKV that.replace('#', '%23') - - # # yarr, have to do options separately or everything goes to shit - # options = delete data.options - # graph.model.set data, {+silent} - # graph.chartOptions options, {+silent} - # # Flush all changes once the DOM settles - # _.delay do - # -> - # graph.change() - # # graph.scaffold.invoke 'change' - # 50 + # Extract slug from URL + if match = /\/graph\/(?!view)([^\/?]+)/i.exec loc + data.slug = match[1] vis = root.vis = new VisModel data graph = root.graph = new VisView do diff --git a/lib/server/server.co b/lib/server/server.co index b4cf71c..44c8d93 100755 --- a/lib/server/server.co +++ b/lib/server/server.co @@ -132,7 +132,7 @@ app.get '/graph/:slug/?', (req, res, next) -> # console.log ' slug: ', slug # console.log ' params:', req.params unless _.str.include slug, '.' - {pathname, search} = req.info + {pathname, search or ''} = req.info req.url = "#pathname/view#search" req.params.action = 'view' next() diff --git a/lib/vis/vis-model.co b/lib/vis/vis-model.co index c534e1d..380bfc0 100644 --- a/lib/vis/vis-model.co +++ b/lib/vis/vis-model.co @@ -89,8 +89,9 @@ VisModel = exports.VisModel = BaseModel.extend do # {{{ json - toKVPairs: -> + toKVPairs: (keepSlug=false) -> kvo = @toJSON() + delete kvo.slug unless keepSlug # console.group 'toKVPairs' # console.log '[IN]', JSON.stringify kvo opts = kvo.options = _.clone kvo.options @@ -105,6 +106,17 @@ VisModel = exports.VisModel = BaseModel.extend do # {{{ # console.groupEnd() _.collapseObject kvo + toKV: (keepSlug=false) -> + _.toKV @toKVPairs keepSlug + + + /** + * @returns {String} URL identifying this model. + */ + toURL: -> + slug = @get 'slug', '' + slug = "/#slug" if slug + "/graph#slug?#{@toKV(false)}" toString: -> "#{@ctorName}(id=#{@id})" diff --git a/lib/vis/vis-view.co b/lib/vis/vis-view.co index 6b13eba..8e3e86c 100644 --- a/lib/vis/vis-view.co +++ b/lib/vis/vis-view.co @@ -68,7 +68,7 @@ VisView = exports.VisView = BaseView.extend do # {{{ @scaffold.collection.reset that if o.graph_spec @scaffold.on 'change', (scaffold, value, key, field) ~> - console.log "scaffold.change!", key, value, field + # console.log "scaffold.change!", key, value @model.setOption key, value, {+silent} #unless field.isDefault() options = @model.get 'options', {} -- 1.7.0.4