From 68ce109cc1609b7277a1afebcdee1cd424d8e9a9 Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Wed, 11 Dec 2013 14:17:31 +0200 Subject: web client: refresh filtered reference choices on field updates --- acf2/model/init.lua | 11 +++++++---- protocol.txt | 2 ++ web/client.js | 40 ++++++++++++++++++++++++++++++++++------ 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/acf2/model/init.lua b/acf2/model/init.lua index c9d95f9..29258a3 100644 --- a/acf2/model/init.lua +++ b/acf2/model/init.lua @@ -55,7 +55,9 @@ local isinstance = object.isinstance local super = object.super local pth = require('acf2.path') + local util = require('acf2.util') +local update = util.update local stringy = require('stringy') @@ -105,7 +107,7 @@ function M.Reference:_choice(context) end end - else util.update(ch, {value=pth.escape(v), ['ui-value']=v}) end + else update(ch, {value=pth.escape(v), ['ui-value']=v}) end if ch then table.insert(res, ch) end end @@ -114,9 +116,10 @@ function M.Reference:_choice(context) end function M.Reference:meta(context) - local res = super(self, M.Reference):meta(context) - res.scope = self:abs_scope(context) - return res + return update( + super(self, M.Reference):meta(context), + {scope=self:abs_scope(context), dynamic=self.filter and true or false} + ) end function M.Reference:follow(context, value) diff --git a/protocol.txt b/protocol.txt index eb03a4b..f65a93e 100644 --- a/protocol.txt +++ b/protocol.txt @@ -68,6 +68,8 @@ resp: JSON object, with the following attributes: - ui-value (user-friendly version) - ref (absolute path to the referred object, not defined if refers to a primitive value) + - dynamic: boolean indicating whether choices may change + based on other fields - fields (model only): array of field metadata JSON objects - members (collections and sets only): metadata for diff --git a/web/client.js b/web/client.js index b3261cb..6ccb917 100644 --- a/web/client.js +++ b/web/client.js @@ -138,6 +138,10 @@ $(function() { return data.data[index(name)]; }; + data.metaRequest = function(name) { + return request("/meta" + join(path, name)); + }; + data.match = function(filter) { if (!filter) return true; return _.every(_.map(filter, function(values, key) { @@ -498,6 +502,7 @@ $(function() { init: function(data, name, meta, level, editable, removable) { this.data = data; + this.name = name; this.meta = meta; this.level = level; @@ -511,14 +516,20 @@ $(function() { } this.makeEl(); + this.dynamic = meta.dynamic; var self = this; - var request = this.requestData(value, meta) - .done(function(value, meta) { + var request; + function handleRequest(req) { + request = req; + req.done(function(value, meta) { + if (req != request) return; self.render(value, meta); self.setStatus(status); }); + } + handleRequest(this.requestData(value, meta)); this.wrapped = this.wrap(); this.visible = true; @@ -548,7 +559,9 @@ $(function() { }); this.wrapped.on("updated", function(event, field) { + if (self.dynamic) handleRequest(self.refreshData()); if (!field || + self.dynamic || (meta.condition && field in meta.condition)) validate(); event.stopPropagation(); @@ -563,6 +576,15 @@ $(function() { return $.Deferred().resolve(value, meta); }, + refreshData: function() { + var def = $.Deferred(); + var self = this; + this.data.metaRequest(this.name).done(function(data) { + def.resolve(self.get(), data); + }); + return def; + }, + wrap: function() { return this.el; }, showStatus: true, @@ -588,7 +610,6 @@ $(function() { init: function( data, name, meta, level, editable, removable ) { - this.name = name; this.editable = editable && meta.editable; var el = this.super( @@ -686,7 +707,7 @@ $(function() { createEl: function() { return $("