Models in services.js


#1

Hi,
I’m fairly new to angular and ionic.

I’m trying to represent a model in my ionic App – and AddressBook and contacts.
the approach i’m taking right now is to create them in services.js using the factory method on the module

so i have

angular.module('blah.services', ['ionic.utils'])
.factory('Contact', function($q, $log){
    var o = {
            "name" : '',
            "email" : '',
            "dt_added" : null
    };
    return o;
})
.factory('Contacts', function($q, $log, Contact){
});

My questions:

  • is am I doing this wrong? should i be declaring the object Contact somwhere else and/or some other way?
  • If i want to instantiate Contact, is there a way I can pass it starting parameters like a constructor? i.e. something like var x = Contact(‘some name’, ‘some email’);

Thanks


#2

“In a way, the architecture of Angular kind of assumes you are getting your models from the backend (a server, webservice, etc) via $http.”


#3

thanks yes,
I saw that answer while googling around on this topic. I don’t completely understand if i’m on the right track or not.

Can you help answer in the context of the two questions I posed in my original question?

Thanks


#4

I suggest you stop right where you are and read up more on AngularJS modules and factories and then Ionic. Your code has a lot of issues and will never work. This is not how AngularJS factories work, and to be honest, the code you posted makes no sense.

Here are some links to hopefully get you on the right path:

https://docs.angularjs.org/guide/providers

Your issue deals more with the usage and understanding of AngularJS and not Ionic. Read the links I’ve posted here. I think they will help.

~ Brad


#5

I think there are differing opinions here… this blog post spends some time explaining one person’s opinion

Models and Services in Angular - Rob ConeryRob Conery http://bit.ly/1FmvQwp


#6

Just wanted to say thanks for the link aaronksaunders.
helpful post.


#7

no problem… the community around a project like this is what helps make us all better and the project - Ionic better!