Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Re : Trouble trying to create a CanJS small widget

$
0
0
Thanks for the solution :D About an hour or so ago, I managed to find the solution on my own, which is quite similar to yours (below is the fully working widget with some pretty nifty validation on top of it, if anybody needs another grid example):

  1. (function ($, can, moment) {

  2.     var DosareDauna = can.Model.extend({
  3.         findAll: "/conta/Plati/GetDosareForSelectie"
  4.     }, {
  5.         setValoareDeExecutat: function (valoare, success, error) {
  6.             if (isNaN(valoare)) {
  7.                 error('Sumele trebuie formate din cifre!');
  8.                 return valoare;
  9.             }
  10.             else {
  11.                 var valoareParsata = parseFloat(valoare);
  12.                 if (valoareParsata > 0) {
  13.                     success(valoareParsata);
  14.                 }
  15.                 else
  16.                     error('Suma executata pt un dosar trebuie sa fie mai mare decat 0!');
  17.                 return valoareParsata;

  18.             }
  19.         }
  20.     });

  21.     can.Component.extend({
  22.         tag: "executadosare",
  23.         //template: can.view("selecteazaDosare"),
  24.         init: function () {
  25.             var self = this.scope;
  26.             var promise = DosareDauna.findAll({ id: self.attr('selectieid') });
  27.             promise.then(function (dosare) {
  28.                 dosare.forEach(function (element, index, list) {
  29.                     element.attr('ValoareDeExecutat', element.attr('SumaMaxima'))
  30.                     element.attr('IsSelected', false);
  31.                     element.attr('EsteFinal', false);
  32.                     element.bind("error", function (ev, attr, error) {
  33.                         if (self.attr('Errors.Dosar') === undefined)
  34.                             self.attr('Errors').attr('Dosar', error);
  35.                     });
  36.                     element.bind("success", function (ev, attr, success) {
  37.                         if (self.attr('Errors.Dosar') !== undefined)
  38.                             self.attr('Errors').removeAttr('Dosar');
  39.                     });
  40.                 });
  41.                 self.attr('Count', dosare.length);
  42.                 self.attr('ToateDosarele').replace(dosare);
  43.                 self.attr('sumaMaxima', dosare.sumaMaxima);

  44.             });
  45.         },
  46.         scope: {
  47.             Errors: {},
  48.             HasError:function(){
  49.                 var self = this.attr('Errors');
  50.                 console.log(self.attr());
  51.                 if (self.attr('Selectie') === undefined && self.attr('Suma') === undefined && self.attr('Dosar') === undefined)
  52.                         return false;
  53.                     else
  54.                         return true;
  55.             },
  56.             Count: Infinity,
  57.             Offset: 0,
  58.             Limit: 5,
  59.             Prev: function () {
  60.                 this.attr('Offset', this.Offset - this.Limit)
  61.             },
  62.             Next: function () {
  63.                 this.attr('Offset', this.Offset + this.Limit);
  64.             },
  65.             CanNext: function () {
  66.                 return this.attr('Offset') < this.attr('Count') - this.attr('Limit');
  67.             },
  68.             CanPrev: function () {
  69.                 return this.attr('Offset') > 0;
  70.             },
  71.             ToateDosarele: [],
  72.             Dosare: function () {
  73.                 var self = this;
  74.                 var dosarePaginate = [];
  75.                 self.attr('ToateDosarele').forEach(function (element, index, list) {
  76.                     if (index >= self.attr('Offset') && index < (self.attr('Offset') + self.attr('Limit'))) {
  77.                         dosarePaginate.push(element);
  78.                     }
  79.                 });
  80.                 return dosarePaginate;
  81.             },
  82.             DosareSelectate: function () {
  83.                 var self = this;
  84.                 var selectate = new can.List([]);
  85.                 var sumaTotala = 0;
  86.                 self.attr('ToateDosarele').forEach(function (element, index, list) {
  87.                     if (element.attr('IsSelected') === true) {
  88.                         selectate.push(element);
  89.                         sumaTotala = sumaTotala + element.attr('ValoareDeExecutat');
  90.                     }
  91.                 });
  92.                 self.attr('Suma', sumaTotala);
  93.                 return selectate;
  94.             },
  95.             Suma: 0,
  96.             SelectAll: function () {
  97.                 var self = this;
  98.                 self.attr('ToateDosarele').forEach(function (element, index, list) {
  99.                     element.attr('IsSelected', true);
  100.                 });
  101.             },
  102.             FinalAll: function () {
  103.                 var self = this;
  104.                 self.DosareSelectate().forEach(function (element, index, list) {
  105.                     element.attr('EsteFinal', true);
  106.                 });
  107.             }
  108.         },
  109.         events: {
  110.             '#submitExecutie click': function (el, ev) {
  111.                 if (this.validate() === true)
  112.                     ev.preventDefault();
  113.             },
  114.             '{scope} Suma': 'validate',
  115.             validate: function () {
  116.                 var self = this.scope;
  117.                 var hasErrors = false;
  118.                 if (self.attr('Suma') <= 0) {
  119.                     if (self.attr('Errors.Suma') === undefined)
  120.                         self.attr('Errors').attr('Suma', 'Suma trebuie sa fie mai mare decat 0!');
  121.                     hasErrors = true;
  122.                 }
  123.                 else
  124.                     if (self.attr('sumaMaxima') < self.attr('Suma')) {
  125.                         if (self.attr('Errors.Suma') === undefined)
  126.                             self.attr('Errors').attr('Suma', 'Suma trebuie sa fie mai mica decat ' + self.attr('sumaMaxima') + '!');
  127.                         hasErrors = true;
  128.                     }
  129.                     else {
  130.                         if (self.attr('Errors.Suma') !== undefined)
  131.                             self.attr('Errors').removeAttr('Suma');
  132.                     }
  133.                 if (self.DosareSelectate().length === 0) {
  134.                     if (self.attr('Errors.Selectie') === undefined)
  135.                         self.attr('Errors').attr('Selectie', 'Trebuie selectate dosare pt executie!');
  136.                     hasErrors = true;
  137.                 }
  138.                 else {
  139.                     if (self.attr('Errors.Selectie') !== undefined)
  140.                         self.attr('Errors').removeAttr('Selectie');
  141.                     var areNumereAiurea = false;
  142.                     self.DosareSelectate().forEach(function (element, index, list) {
  143.                         if (isNaN(element.attr('ValoareDeExecutat'))) {
  144.                             areNumereAiurea = true;
  145.                         }
  146.                     });
  147.                     if (!areNumereAiurea) {
  148.                         if (self.attr('Errors.Dosar') !== undefined)
  149.                             self.attr('Errors').removeAttr('Dosar');
  150.                     }
  151.                     else
  152.                         if (self.attr('Errors.Dosar') === undefined)
  153.                             self.attr('Errors').attr('Dosar', 'Sumele trebuie formate din cifre!');
  154.                 }
  155.                 return hasErrors;
  156.             }

  157.         },
  158.         helpers: {
  159.             // {{date-format 'unix offset, date string or timestamp' 'mm-dd-yy'}}
  160.             "date-format": function (time) {
  161.                 if (can.isFunction(time)) time = time();
  162.                 return moment(time).format('YYYY MM DD');
  163.             }
  164.         }
  165.     });
  166. }(jQuery, can, moment));

Things I've learned:

1. When passing parameters through a custom Component tag, always use lowercase letters for the scope property.

2. Functions automatically become computes IF they are referenced in the Mustache view, otherwise if they're simply called by the code, they must be enclosed in a can.compute(..)

3. When setting if else clauses for things like disabled="disabled", don't leave any whitespace.

At this point, in order to make the code better, I'd push the validation inside the object that is returned by the deferred, and set it up so that it can push errors in the widget that it is passed.

In any case, thanks for the help, air_hadoken :D


Viewing all articles
Browse latest Browse all 3491

Trending Articles