0

My code:

webApp.controller ('entityCtrl', function ($scope, Entity) {  
    $scope.x = new Entity('1','3343','32434');
});


webApp.factory('Entity',function(){

  var _id,
      _created,
      _updated;


  //constructor

  function Entity(id,created,updated){


      this._id = id;
      this._created = created;
      this._updated = updated;

      return this;
  }

  var save = function (){
      console.log('save');
  };

  var update = function () {
      console.log('update');
  };

  var _delete = function () {
      console.log('delete');
};

  return {

      save: save,
      update: update,
      delete: _delete
  }

});

The Error I get:

TypeError: object is not a function
at new <anonymous> (http://localhost/webApp/trunk/htdocs/js/rw/controllers.js:12:16)
at invoke (http://localhost/webApp/trunk/htdocs/js/vendors/angular.js:2902:28)
at Object.instantiate (http://localhost/webApp/trunk/htdocs/js/vendors/angular.js:2914:23)
at $get (http://localhost/webApp/trunk/htdocs/js/vendors/angular.js:4805:24)
at $get.i (http://localhost/webApp/trunk/htdocs/js/vendors/angular.js:4384:17)
at forEach (http://localhost/webApp/trunk/htdocs/js/vendors/angular.js:137:20)
at nodeLinkFn (http://localhost/webApp/trunk/htdocs/js/vendors/angular.js:4369:11)
at compositeLinkFn (http://localhost/webApp/trunk/htdocs/js/vendors/angular.js:4015:15)
at compositeLinkFn (http://localhost/webApp/trunk/htdocs/js/vendors/angular.js:4018:13)
at publicLinkFn (http://localhost/webApp/trunk/htdocs/js/vendors/angular.js:3920:30)    angular.js:5754

What am I doing wrong?

1 Answer 1

2

This has actually little to do with Angular. You're returning an Object (as the error says) from the method. You may want to return your Entity function.

Consider this example:

webApp.controller ('entityCtrl', function ($scope, Entity) {  
    $scope.x = new Entity('1','3343','32434');
});

webApp.factory('Entity',function(){
  var Entity = function(id,created,updated){
      this._id = id;
      this._created = created;
      this._updated = updated;
  };
  Entity.prototype.save = function (){
      console.log('save');
  };
  Entity.prototype.update = function () {
      console.log('update');
  };
  Entity.prototype._delete = function () {
      console.log('delete');
  };

  return Entity;
});

Btw. you may want to watch this video, on how the Object Oriented JS works : http://youtu.be/PMfcsYzj-9M

Sign up to request clarification or add additional context in comments.

7 Comments

Throws me a new error TypeError: Object function (id,created,updated){ this._id = id; this._created = created; this._updated = updated;
Cannot be ;) : checkout this Plunkr plnkr.co/edit/ygi5OePQwSkLQt8Uthi1?p=preview
Yes you are right, there seems to be problem displaying {{x}}, anyway it works thanks man!
By the way, I read a little about JS OOP, can you explain why to use prototype? Isn't that just a way to define a public methods?
Not really, JS doesn't have explicit way to define visibility (like Java does for example), it is a way to define inheritance - please check the link I've attached.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.