comparison static/add.js @ 10:e054949143e9

reworking addlink and shareWith support Ignore-this: b1665b776f3964f7fde219acadc51f32
author drewp@bigasterisk.com
date Fri, 15 Mar 2013 00:29:53 -0700
parents f8c4c7ce5f4a
children 9fc1f5c8aa4e
comparison
equal deleted inserted replaced
9:7df920c18c83 10:e054949143e9
1 var model = { 1 var model = {
2 href: ko.observable(""), 2 linkRecord: {
3 description: ko.observable(""), 3 href: ko.observable(""),
4 tag: ko.observable(""), 4 description: ko.observable(""),
5 extended: ko.observable(""), 5 tag: ko.observable(""),
6 extended: ko.observable(""),
7 private: ko.observable(false),
8 shareWith: ko.observableArray([]), // foaf uris
9 },
6 submitLabel: ko.observable("Add"), 10 submitLabel: ko.observable("Add"),
7 }; 11 };
8 12
9 ko.computed(function() { 13 ko.computed(function() {
10 if (model.href() == "") { 14 if (model.linkRecord.href() == "") {
11 return; 15 return;
12 } 16 }
13 17
14 $.getJSON("addLink/proposedUri", {uri: model.href()}, function (data) { 18 $.getJSON("addLink/proposedUri", {uri: model.linkRecord.href()}, function (data) {
15 // these could arrive after the user has started typing in the fields! 19 // these could arrive after the user has started typing in the fields!
16 20
17 model.description(data.description); 21 model.linkRecord.description(data.description);
18 model.tag(data.tag); 22 model.linkRecord.tag(data.tag);
19 model.extended(data.extended); 23 model.linkRecord.extended(data.extended);
20 24 model.linkRecord.shareWith(data.shareWith);
21 model.submitLabel(data.existed ? "Update existing" : "Add"); 25 model.submitLabel(data.existed ? "Update existing" : "Add");
22 26
23 }); 27 });
24 28
25 }); 29 });
26 30
27 ko.applyBindings(model); 31 ko.applyBindings(model);
32
33 $("#shareWith").select2({
34 tokenSeparators: [",", " "],
35 ajax: {
36 url: "/foaf/findPerson",
37 data: function (term, page) {
38 return {q: term};
39 },
40 results: function (data, page) {
41 var ret = {results: data.people.map(
42 function (row) {
43 return {id: row.uri, text: row.label + " ("+row.uri+")"}
44 }),
45 more: false,
46 context: {}
47 };
48 //ret.results.push({id: "new1", text: this.context});
49 return ret;
50 }
51 },
52 tags: [],
53 });
54 $("#shareWith").on('change', function (e) { setModelFromShares(e.val); });
55
56 var setSharesFromModel = ko.computed(
57 function () {
58 var uris = ko.utils.arrayGetDistinctValues(model.linkRecord.shareWith());
59 console.log("from model", uris)
60 $("#shareWith").select2("data", uris.map(
61 function (uri) {
62 return {id: uri, text: "("+uri+")"};
63 }));
64 });
65
66 function setModelFromShares(n) {
67 console.log("from val", $("#shareWith").select2("val"), "new", n)
68 model.linkRecord.shareWith($("#shareWith").select2("val"));
69 }
70
71 setSharesFromModel();