Skip to content

Instantly share code, notes, and snippets.

@tucaz
Created October 14, 2011 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tucaz/1285950 to your computer and use it in GitHub Desktop.
Save tucaz/1285950 to your computer and use it in GitHub Desktop.
Modules + PubSub
//Module
Widget = function (core) {
var Entry = function (entryText, accountId) {
this.EntryText = entryText;
this.AccountId = accountId;
};
var EntriesViewModel = function () {
var that = this;
this.selectedAccount = null;
this.allEntries = new ko.observableArray();
this.newEntry = new ko.observable();
this.getAllEntries = function (accountId) {
that.selectedAccount = accountId;
$.getJSON(getAllEntriesUrl + accountId, null, function (data, textStatus, xhr) {
that.allEntries.removeAll();
$.each(data, function (index, item) {
var existingEntry = new Entry(item.EntryText);
that.allEntries.push(existingEntry);
});
});
};
this.addEntry = function () {
if ((this.newEntry() !== '')) {
var entry = new Entry(this.newEntry(), that.selectedAccount);
$.post(Nomos.Routes.addEntryUrl, entry, function (data, textStatus, xhr) {
core.publish('/entry/new', [entry]);
that.allEntries.push(entry);
that.newEntry('');
}, 'JSON');
}
};
};
var entriesVM = new EntriesViewModel();
return {
init: function () {
ko.applyBindings(entriesVM, document.getElementById('entries'));
core.subscribe('/account/changed', entriesVM.getAllEntries);
},
destroy: function () {
entriesVM = null;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment