1

Hello my problem is simple but I lost whole day and I can't find the solution

I have JAVA objects

class Phone {
     public int id;
     public String name;
     public List<Accessory> accessories;
}

and

class Accessory {
    public int id;
    public String name;
    public List<Phone> phones;
}

how should I map this in js ? I can't find any reference to creating maping of relation between objects

if I manualy create something like this

name: nokia,
accesories: {
id:1, name: head,
id:2, name: keys
}

i won't get relation i'll just get a big model

// edit

function MyCtrl($scope) {
        $scope.Phone = {Id: 1, Name:'Nokia'};
        $scope.Phone.Accessories = [{Id:1, Name:'foo'}, {Id:2, Name:'bar'}, {Id:3, Name:'barfoo'}];
    }

this is code from your url. But this ain't close enough

I would like to have something like this

$scope.Phones = [{Id: 1, Name:'Nokia'}, {Id: 2, Name:'Samsung'}, {Id: 3, Name:'iPhone'}];

But I don't know how to bind accessories to each of the phones

Fixus
  • 4,631
  • 10
  • 38
  • 67
  • I see a one to many relation? Phone with multiple Accessories. And Accesory does not has a list of phones. But how do you want the Javascript would look like? And what does this have to do with angular? – Martijn Aug 22 '13 at 11:42
  • @Martijn I've updated the code. I don't care how the js code will look like :) I want to make a map from JAVA to angular model so when I update on site Accessory or Phone this will have reflection in models on site. For example in emberjs when I create an array of keys (only keys) the models are bound I can work on them like I would in JAVA. It has everything to angular cause I don't know how to map relation in angularjs – Fixus Aug 22 '13 at 11:49
  • Okay. Well, as dabee already mentioned, there is no relation or link between angular and Java. You need to create javascripts objects and bind to these objects in Angular. For my current project I use MVC and those methods returns for example e list of json objects, which I then use for binding. – Martijn Aug 22 '13 at 12:12
  • @Martijn it is possible that i'm thinking to wide. Where I can find binding in Angular between objects that have relations between them ? – Fixus Aug 22 '13 at 12:22
  • 1
    I've create a fiddle, which will hopefully answer your question: http://jsfiddle.net/YaRHt/1/ Here you have a Phone with a list of accessories. – Martijn Aug 22 '13 at 12:36
  • @Martijn that is close but not enought. I've updated my post using your code as example to show what I want to achive – Fixus Aug 22 '13 at 12:47
  • 1
    Can't you just create the complete object graph in Java and convert that graph to Json and sent that back? – Martijn Aug 22 '13 at 13:22
  • @Martijn thats the point. sorry for newbie questions but I'm new at this. I'm still learning the concept – Fixus Aug 22 '13 at 14:36
  • In that case, I think you need to rephrase your question. I should find out how to convert your object graph to Json in Java. Unfortunately I can't help you because I'm a .NET guy :) Goodluck! PS Maybe this helps: http://stackoverflow.com/questions/11001458/json-java-object-to-json – Martijn Aug 22 '13 at 20:09

1 Answers1

1

There's nothing like class-level definition for such relation in Javascript (neither in AngularJS). Look at data-binding in AngularJS, I think that's what you're describing in comments. http://docs.angularjs.org/guide/dev_guide.templates.databinding

bdavidxyz
  • 2,492
  • 1
  • 20
  • 40