var documentsUrl = '/api/documents/';
-var DocumentModel = Class.extend({
+
+var Model = Class.extend({
+ observers: {},
+
+ init: function() {},
+
+ signal: function(event, data) {
+ console.log('signal', this, event, data);
+ if (this.observers[event]) {
+ for (observer in this.observers[event]) {
+ observer.handle(event, data);
+ }
+ };
+ return this;
+ },
+
+ addObserver: function(observer, event) {
+ if (!this.observers[event]) {
+ this.observers[event] = [];
+ }
+ this.observers[event][observer] = observer;
+ return this;
+ },
+
+ removeObserver: function(observer, event) {
+ if (!event) {
+ for (e in this.observers) {
+ this.removeObserver(observer, e);
+ }
+ } else {
+ delete this.observers[event][observer];
+ }
+ return this;
+ }
+});
+
+
+var XMLModel = Model.extend({
+ parent: null,
+ data: null,
+ serverURL: null,
+
+ init: function(parent, serverURL) {
+ this.parent = parent;
+ this.serverURL = serverURL;
+ },
+
+ reload: function() {
+ $.ajax({
+ url: this.serverURL,
+ dataType: 'text',
+ success: this.reloadSucceeded.bind(this)
+ });
+ },
+
+ reloadSucceeded: function(data) {
+ this.data = data;
+ this.signal('reloaded');
+ },
+})
+
+var DocumentModel = Model.extend({
data: null, // name, text_url, latest_rev, latest_shared_rev, parts_url, dc_url, size
- xml: '',
- html: '',
+ xml: null,
+ html: null,
init: function() {},
successfulGetData: function(callback, data) {
this.data = data;
- this.modelChanged();
+ this.signal('changed');
if (callback) {
(callback.bind(this))(data);
}
},
- getXML: function(callback) {
- $(this).trigger('modelxmlfreeze');
+ load: function(key) {
if (!this.data) {
- this.getData(this.getXML);
+ this.getData(function() { this.load(key); }.bind(this));
} else {
- console.log('getXML:', this.data.text_url);
+ console.log('load', key, this.data[key + 'url']);
$.ajax({
- cache: false,
- url: this.data.text_url,
+ url: this.data[key + '_url'],
dataType: 'text',
- success: this.successfulGetXML.bind(this, callback)
+ success: this.loadSucceeded.bind(this, key)
});
- };
+ }
},
- successfulGetXML: function(callback, data) {
- if (data != this.xml) {
- this.xml = data;
- this.modelChanged();
- $(this).trigger('modelxmlchanged');
+ loadSucceeded: function(key, data) {
+ console.log('loadSucceeded', key, data);
+ this.set(key, data);
+ },
+
+ get: function(key) {
+ console.log(this[key]);
+ if (this[key]) {
+ return this[key]
+ } else {
+ this.load(key);
+ return '';
}
- $(this).trigger('modelxmlunfreeze');
},
- modelChanged: function() {
- $(this).trigger('modelchanged');
+ set: function(key, value) {
+ this[key] = value;
+ this.signal(key + '-changed');
+ return this;
}
});