9

I am trying to implement MVC using AMD in canjs. For that I am using requirejs. This is my domains.json file:

[            
"1":{"uid":     "1","urls": "domain1.abc.com"},
"2":{"uid":    "2","urls": "domain2.abc.com"},
"3":{"uid":    "3","urls": "domain3.abc.com"}
]

This is my domainModel:

define(['can'], function(can){
  SearchModel= can.Model({
     id: 'uid',
     findAll: 'GET /domains.json'
  },{})
  return SearchModel;
})

This is my controller:

define(['can','jquery'],function(can,$){
 domainController=can.Control({defaults:{view:"../view/search.hbs" }},           
  {
    init : function(element,options){   
        this.element.html(can.view(this.options.view,{
            searchlist : this.options.search
        }))
    }
});
return domainController;
} 

This is my main js:

equirejs(['can','controller/domainController','model/domainModel'],
 function(can, domainController,domainModel) {
   var Application = can.Control.extend({
    defaults :{ }
   },{  
        init: function(element,options){
         console.log('loaded');
         domainModel.findAll({}, function(domains){
            domainObject:{searchdomains : domains}
                 new domainController('#search',domainObject)
            });
        }
    })
return Application;
});

I am tracing out my code.I put breakpoints.On model breakpoints I am not getting values in local variables in chrome devtools.

The url property has 'undefined/{id}' value and findAll method having four properties i.e. arguments,caller,length and name having a value null, null, 0 and "" respectively

I have checked my url of model by navigating through localhost on browser and it is correct. Then why model cannot getting the values of json file?

Louis
  • 146,715
  • 28
  • 274
  • 320
Dipesh Raichana
  • 1,008
  • 3
  • 18
  • 36
  • 1
    Remember object names cannot start with a number, so in the domains.json file, change the names from 1, 2, 3, to something like "one", "two", "three". It is ok to make a json with object names that start with integers but when you try to access these objects in js, it will not work properly. [valid JS variable names](https://mathiasbynens.be/notes/javascript-identifiers) – Jacob Finamore May 13 '15 at 13:56

1 Answers1

5

You should get an error message since your data is not what Model expects for findAll. Your JSON should be an array (or at least have a length property):

[
  {"uid":     "1","urls": "domain1.abc.com"},
  {"uid":    "2","urls": "domain2.abc.com"},
  {"uid":    "3","urls": "domain3.abc.com"}
]

You also probably want to set the id property in you SearchModel to uid:

define(['can'], function(can){
  SearchModel= can.Model({
     id: 'uid',
     findAll: 'GET /domains.json'
  },{})
  return SearchModel;
})
Daff
  • 43,734
  • 9
  • 106
  • 120
  • I already tried with array. Its not working. Is it important to set id property in model? if yes can you please tell me the reason. – Dipesh Raichana May 05 '15 at 17:46
  • Well it needs to know what the models id property is. That shouldn't be the problem though. Hat you tried `{ "data": [] }` in your JSON file? Can you see it requesting the correct file in the network tab? – Daff May 13 '15 at 21:27
  • thank you so much. I didn't know that "data" is a keyword which has to be used in the JSON. I was using different word. You guys are doing great work. Please keep it going!! – Dipesh Raichana May 14 '15 at 05:39
  • This might actually be a Chrome safety restriction. Thanks! And glad it worked. Feel free to drop by https://gitter.im/bitovi/canjs if you have more questions. – Daff May 14 '15 at 14:58