How to create multiple shared models

3,241 views
Skip to first unread message

ganaraj p r

unread,
May 10, 2012, 8:08:29 PM5/10/12
to ang...@googlegroups.com
I have come to understand that if you want to share the same model with multiple views a good practice would be to create a service and then share that service between the different views. 

Now since a service is a singleton, how do I go about creating 'n' models? Am I missing something really obvious here?

If I want to create 'n' instances of a model and each model needs to be shared between more than 1 view, how do we go about it? 

--
Regards,
Ganaraj P R

John Lindquist

unread,
May 10, 2012, 8:20:07 PM5/10/12
to ang...@googlegroups.com
I'm not exactly sure what you're asking... creating models on a service then sharing it across "views" (do you mean injecting into a controller then exposing data to the dom through the scope)?




--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

ganaraj p r

unread,
May 10, 2012, 8:34:54 PM5/10/12
to ang...@googlegroups.com
If you are creating a html editor, how would you go about building the model for it? 

Consider a simplified version of this problem . Lets just take div's. 

I need a model to hold the data corresponding to each div. This model will hold all the attributes for that div say. Ofcourse, in a html document you arent restricted to creating 1 div. So you will need to be able to create 'n' models... 1 for each div you want to create.

Since you are building the editor you will need 2 views for each of the html elements. 

1. Edit view - A view where you can use input elements to fill in values for each of the attributes for a node. 
2. Preview view - Showing / graphically representing the node itself. 

So, given this scenario, I was thinking I would need a way of creating multiple models. 

Eddie

unread,
May 10, 2012, 8:38:53 PM5/10/12
to ang...@googlegroups.com
If you want to share model between 2 controllers, easiest way is to put an object under common parent scope

eg:

$rootScope.sharedState = {
html: "<div></div>"
};

then have both controller refer to the fields

eg: {{sharedState.html}}


On Thursday, May 10, 2012 1:34:54 PM UTC-7, ganaraj p r wrote:
If you are creating a html editor, how would you go about building the model for it? 

Consider a simplified version of this problem . Lets just take div's. 

I need a model to hold the data corresponding to each div. This model will hold all the attributes for that div say. Ofcourse, in a html document you arent restricted to creating 1 div. So you will need to be able to create 'n' models... 1 for each div you want to create.

Since you are building the editor you will need 2 views for each of the html elements. 

1. Edit view - A view where you can use input elements to fill in values for each of the attributes for a node. 
2. Preview view - Showing / graphically representing the node itself. 

So, given this scenario, I was thinking I would need a way of creating multiple models. 

On Thu, May 10, 2012 at 9:20 PM, John Lindquist <johnli...@gmail.com> wrote:
I'm not exactly sure what you're asking... creating models on a service then sharing it across "views" (do you mean injecting into a controller then exposing data to the dom through the scope)?


On Thu, May 10, 2012 at 2:08 PM, ganaraj p r <gana...@gmail.com> wrote:
I have come to understand that if you want to share the same model with multiple views a good practice would be to create a service and then share that service between the different views. 

Now since a service is a singleton, how do I go about creating 'n' models? Am I missing something really obvious here?

If I want to create 'n' instances of a model and each model needs to be shared between more than 1 view, how do we go about it? 

--
Regards,
Ganaraj P R

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/angular?hl=en.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/angular?hl=en.

ganaraj p r

unread,
May 10, 2012, 10:33:50 PM5/10/12
to ang...@googlegroups.com
Consider Backbone Models. You can create multiple instances of a backbone model. 

Basically I want to create a "class" of a model and want to do a "new" on that class each time I want a model.

I tried $provider.provide and wrote the provider something like this :

function NodeProvider() {

    var Node = {
        name :"",
        nodes:[],
        attributes:[],
        addAttributes:function(name,value)
        {
            this.attributes.push({nodeName:name,nodeValue:value});
        },
        addNode:function(name)
        {
            this.nodes.push({name: name,nodes: [],attributes:[]});
        },
        deleteNode:function()
        {
            this.nodes = [];
        }
    };
    this.$get(function(){
        return new Node();
    });
}


with the assumption that each time I call the provider it would give me a new node! But alas, it says Node is not a constructor!


To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/P9letYDjN7sJ.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/angular?hl=en.
Message has been deleted

Andy Joslin

unread,
May 11, 2012, 12:23:05 AM5/11/12
to ang...@googlegroups.com
Hi Ganaraj,

remember, in Javascript to declare an object you have to make it a function.  This should work the way you want it:
 
function NodeProvider({

    function Node({
        this.name "";
        this.nodes =[];
        this.attributes [];
        this.addAttributes function(name,value{

            this.attributes.push({nodeName:name,nodeValue:value});
        };
        this.addNode function(name{
            this.nodes.push(namenamenodes[]attributes:[});
        };
        this.deleteNode function({

Vojta Jína

unread,
May 21, 2012, 2:53:26 AM5/21/12
to ang...@googlegroups.com
Hey ganaraj,

check out this simple app (wReader), its "items" service is a model,
that's shared between multiple controllers:
https://github.com/vojtajina/WebApp-CodeLab/blob/angularjs/FinalProject/js/services.js#L62

Here's live demo of the app:
http://vojtajina.github.com/WebApp-CodeLab/FinalProject/index.html


V.
>>>>> angular+u...@googlegroups.com.
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/angular?hl=en.
>>>>
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "AngularJS" group.
>>>> To post to this group, send email to ang...@googlegroups.com.
>>>> To unsubscribe from this group, send email to
>>>> angular+u...@googlegroups.com.
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/angular?hl=en.
>>>
>>>
>>>
>>>
>>> --
>>> Regards,
>>> Ganaraj P R
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "AngularJS" group.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msg/angular/-/P9letYDjN7sJ.
>>
>> To post to this group, send email to ang...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> angular+u...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/angular?hl=en.
>
>
>
>
> --
> Regards,
> Ganaraj P R
>
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to

ganaraj p r

unread,
May 21, 2012, 9:12:05 AM5/21/12
to ang...@googlegroups.com
Hey thank you :)

That was really cool to know.. 

Actually I figured out something myself. I am using a provider in a service.


I dont really know if this is the right way of doing it. But so far it has worked and has worked well. If someone has a better way of doing this, do let me know :)
Reply all
Reply to author
Forward
0 new messages