comparison static/add.js @ 19:9fc1f5c8aa4e

working on the 'share' selectbox ui Ignore-this: 5e6f80568920eb3aab9e56bdfb509d19
author Drew Perttula <drewp@bigasterisk.com>
date Sun, 17 Mar 2013 00:49:27 -0700
parents e054949143e9
children 8008ec2fd763
comparison
equal deleted inserted replaced
18:7ef5365591fd 19:9fc1f5c8aa4e
28 28
29 }); 29 });
30 30
31 ko.applyBindings(model); 31 ko.applyBindings(model);
32 32
33 $("#shareWith").select2({ 33 (function (inputElem, model) {
34 tokenSeparators: [",", " "], 34 inputElem.select2({
35 ajax: { 35 tokenSeparators: [",", " "],
36 url: "/foaf/findPerson", 36 ajax: {
37 data: function (term, page) { 37 url: "/foaf/findPerson",
38 return {q: term}; 38 data: function (term, page) {
39 return {q: term};
40 },
41 results: function (data, page) {
42 var ret = {
43 results: data.people.map(
44 function (row) {
45 return {id: row.uri,
46 text: row.label + " ("+row.uri+")"};
47 }),
48 more: false,
49 context: {}
50 };
51 //ret.results.push({id: "new1", text: this.context});
52 return ret;
53 }
39 }, 54 },
40 results: function (data, page) { 55 tags: [],
41 var ret = {results: data.people.map( 56 });
42 function (row) { 57 inputElem.on('change', function (e) {
43 return {id: row.uri, text: row.label + " ("+row.uri+")"} 58 console.log("onchange", inputElem.select2('val'));
44 }), 59 setModelFromShares(inputElem.select2('val'));
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 }); 60 });
65 61
66 function setModelFromShares(n) { 62 var enableModel = true;
67 console.log("from val", $("#shareWith").select2("val"), "new", n)
68 model.linkRecord.shareWith($("#shareWith").select2("val"));
69 }
70 63
71 setSharesFromModel(); 64 var setSharesFromModel = ko.computed(
65 function () {
66 var uris = ko.utils.arrayGetDistinctValues(model.linkRecord.shareWith());
67 if (!enableModel) {
68 return;
69 }
70 console.log("from model", uris)
71
72 inputElem.select2("data", uris.map(
73 function (uri) {
74 return {id: uri, text: "("+uri+")"};
75 }));
76 });
77
78 function setModelFromShares(n) {
79 console.log("from val", inputElem.select2("val"), "new", n)
80 enableModel = false;
81 model.linkRecord.shareWith(inputElem.select2("val"));
82 enableModel = true;
83 }
84
85 // setSharesFromModel();
86 })($("#shareWith"), model);
87
88