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

Canjs and jquery problem

$
0
0
Hi,
I'm experiencing a strange problem with canjs 1.1.4 and JQuery 1.9.1.
I see a
when I create a list of objects, using fixture, and try to access to custom methods of the list
I try to explain what I'm doing with code :)

In my html I included:
  1.     <script src="js/vendor/jquery-1.9.1.js"></script>
  2.     <script src="js/plugins.js"></script>
  3.     <script src="js/vendor/can.js/can.jquery.js"></script>
  4.     <script src="js/vendor/can.js/can.fixture.js"></script>
  5.     <script src="js/try.js"></script>

in try.js I extended List object for the model, adding two methods, and used It in Control and viewed in an ejs:

  1. (function(){
  2.     var CONTACTS = [
  3.         {
  4.             id: 1,
  5.             name: 'William',
  6.             address: '1 CanJS Way',
  7.             email: 'william@husker.com',
  8.             phone: '0123456789',
  9.             category: 'co-workers'
  10.         },
  11.         {
  12.             id: 2,
  13.             name: 'Laura',
  14.             address: '1 CanJS Way',
  15.             email: 'laura@starbuck.com',
  16.             phone: '0123456789',
  17.             category: 'friends'
  18.         },
  19.         {
  20.             id: 3,
  21.             name: 'Lee',
  22.             address: '1 CanJS Way',
  23.             email: 'lee@apollo.com',
  24.             phone: '0123456789',
  25.             category: 'family'
  26.         }
  27.     ];
  28.     var CATEGORIES = [
  29.         {
  30.             id: 1,
  31.             name: 'Family',
  32.             data: 'family'
  33.         },
  34.         {
  35.             id: 2,
  36.             name: 'Friends',
  37.             data: 'friends'
  38.         },
  39.         {
  40.             id: 3,
  41.             name: 'Co-workers',
  42.             data: 'co-workers'
  43.         }
  44.     ];
  45.     //MODEL
  46.     Contact = can.Model({
  47.         findAll:  "GET /contacts",
  48.         create  : "POST /contacts",
  49.         update  : "PUT /contacts/{id}",
  50.         destroy : "DELETE /contacts/{id}"
  51.     },{});
  52.     Contact.List = can.Model.List({
  53.         filter: function(category){
  54.             this.attr('length');
  55.             var contacts = new Contact.List([]);
  56.             this.each(function(contact){
  57.                 if(category === 'all' || category === contact.attr('category')) {
  58.                     contacts.push(contact)
  59.                 }
  60.             })
  61.             return contacts;
  62.         },
  63.         count: function(category) {
  64.             return this.filter(category).length;
  65.         }
  66.     });
  67.     Category = can.Model({
  68.         findAll: 'GET /categories'
  69.     },{});
  70.     can.fixture('GET /contacts', function(){
  71.         return [CONTACTS];
  72.     });
  73.     var id= 4;
  74.     can.fixture("POST /contacts", function(){
  75.         return {id: (id++)}
  76.     });
  77.     can.fixture("PUT /contacts/{id}", function(){
  78.         return {};
  79.     });
  80.     can.fixture("DELETE /contacts/{id}", function(){
  81.         return {};
  82.     });
  83.     can.fixture('GET /categories', function(){
  84.         return [CATEGORIES];
  85.     });
  86.     can.route( 'filter/:category' );
  87.     can.route('', {category: 'all' });
  88.     Filter = can.Control({
  89.         init: function(){
  90.             var category = can.route.attr('category') || "all";
  91.             this.element.html(can.view('ejs/filterView.ejs', {
  92.                 contacts: this.options.contacts,
  93.                 categories: this.options.categories
  94.             }));
  95.             this.element.find('[data-category="' + category + '"]').parent().addClass('active');
  96.         },
  97.         '[data-category] click': function(el, ev) {
  98.             this.element.find('[data-category]').parent().removeClass('active');
  99.             el.parent().addClass('active');
  100.             can.route.attr('category', el.data('category'));
  101.         }
  102.     });
  103.     $(document).ready(function(){
  104.         $.when(Category.findAll(), Contact.findAll()).then(
  105.             function(categoryResponse, contactResponse){
  106.                 var categories = categoryResponse[0],
  107.                     contacts = contactResponse[0];
  108.                 new Filter('#filter', {
  109.                     contacts: contacts,
  110.                     categories: categories
  111.                 });
  112.             });
  113.     });
  114. })();
filterView.ejs contains:

  1. <ul class="nav nav-list">
  2.     <% console.log(contacts) %>
  3.     <li class="nav-header">Categories</li>
  4.     <li>
  5.         <a href="javascript://" data-category="all">All (<%= contacts.count('all') %>)</a>
  6.     </li>
  7.     <% categories.each(function(category){ %>
  8.         <li>
  9.             <a href="javascript://" data-category="<%= category.data %>"><%= category.name %> (<%= contacts.count(category.data) %>)</a>
  10.         </li>
  11.     <% }) %>
  12. </ul>
If can help, A similar problem appeared when I tried to access attr function using $.each, instead of canjs.each.
I also tried to use jquery 1.8.3, but the problem is still the same.
Am I doing something wrong?
Thanks for any help.


Viewing all articles
Browse latest Browse all 3491

Trending Articles