Knockout.js Bootstrap MVC Validation DataAnnotation jSon


SUBMITTED BY: Guest

DATE: Jan. 3, 2014, 11:21 p.m.

FORMAT: Text only

SIZE: 2.0 kB

HITS: 1429

  1. //=================================================================
  2. // SUMMARY
  3. //=================================================================
  4. //
  5. // A simple Knockout.js Category viewmodel. Each Category is 1 slide
  6. // of an embedded Twitter Boostrap Carousel. Each Category also lazy
  7. // loads an ObservableArray of Article items, when the Category slide
  8. // becomes active in the Twitter Boostrap Carousel. The lazy loading
  9. // of the Articles is only done once per slide.
  10. var CategoryViewModel = function (parent, jsonCat) {
  11. this.Id = jsonCat.Id;
  12. this.Parent = parent;
  13. this.LoadedArticles = ko.observable(false);
  14. this.Title = ko.observable(jsonCat.Title);
  15. this.Description = ko.observable(jsonCat.Description);
  16. this.Articles = ko.observableArray();
  17. var context = this;
  18. this.hasArticles = ko.computed(function() {
  19. return context.Articles().length > 0;
  20. }, this);
  21. // Shows a modal dialog of the clicked Article
  22. this.articleClicked = function (article) {
  23. context.Parent.SetArticle(article);
  24. };
  25. // Do the lazy load of the article. Basically only do it
  26. // when this fuction is called externally
  27. this.loadArticles = function () {
  28. if (this.LoadedArticles() == true) {
  29. return;
  30. }
  31. this.LoadedArticles(true);
  32. // Push stuff to articles
  33. $.when($.ajax('/api/article/GetAll/' + this.Id))
  34. .then(function (data, textStatus, jqXHR) {
  35. for (var i = 0; i < data.length; i++) {
  36. context.Articles.push(data[i]);
  37. $('#img' + data[i].Id).tooltip({
  38. title: data[i].ShortDescription,
  39. trigger: 'hover focus'
  40. });
  41. }
  42. });
  43. };
  44. };

comments powered by Disqus