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

9
  • 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? Commented Aug 22, 2013 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 Commented Aug 22, 2013 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. Commented Aug 22, 2013 at 12:12
  • 1
    I've create a fiddle, which will hopefully answer your question: jsfiddle.net/YaRHt/1 Here you have a Phone with a list of accessories. Commented Aug 22, 2013 at 12:36
  • 1
    Can't you just create the complete object graph in Java and convert that graph to Json and sent that back? Commented Aug 22, 2013 at 13:22

1 Answer 1

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

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

Comments

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.