0

I'm working on Galleries and Pictures. I'm using Flask, AngularJS, MongoDB (and mongoengine). I displayed all my object thru an API.

I got 2 kinds of objects: - Galleries objects define by id:int, title:string, owner:string, date:string, content:array[], etc... - Pictures objects define by id:int, title:string, owner:string, date:string, etc...

In the Content of Galleries which is an array, I got all the ids belonging of the gallery. [MongoDB sample][1]

When the user click on the concerned gallery, I try to list all the pictures in a HTML page template. For that I'm using a $scope variable in the controller and ng-repeat on the HTML side.

Controller:

    $scope.gallery = Galleries.get({id: $routeParams.id}, function (gallery) {
        for (var i = 0; i < gallery.content.length; i++){
            gallery.picture = Picture.get({id: $scope.gallery.content[i].split('-')[1]});
        }
    });

HTML:

    <tr ng-repeat="pict in gallery">
        <td>{{pict.fileID}}</td>
        <td>{{pict.legend}}</td>
    </tr>

Unfortunately, only the last one picture in the content is displayed. The global variable in the scope isn't feeding at each content index.

How can I do to put all the picture object in an only one scope variable for using ng-repeat in the HTML view?

Thanking in advance, Have a nice day.

2
  • can you show your code of the functions Galleries.get and Picture.get? Commented Sep 9, 2017 at 18:10
  • Hi, it's perhaps little bit tricky, but Could I used an array? Commented Sep 11, 2017 at 7:11

1 Answer 1

0

The get function is providing from the API (mongoengine):

@app.route('/api/galleries/<id>', methods=('GET', 'POST', 'DELETE'))
 def galleries(id):
     return handle_request(Galleries, id)


@app.route('/api/picture/<id>', methods=('GET', 'POST', 'DELETE'))
def picture(id):
    return handle_request(Picture, id)

Then the handle_request looks like:

def handle_request(model, id):
    def get_object():
        try:
            obj = model.objects(id=id).first()
        except:
            raise NotFound
        if not obj:
            raise NotFound
        return obj

    if request.method == 'GET':
        check_allowed('READ', model)
        try:
            return jsonify(get_object().to_dict())
        except NotFound:
            abort(404)
    elif request.method == 'POST':
        check_allowed('UPDATE', model)
        try:
            obj = get_object()
        except NotFound:
            obj = model(id=id)
        obj.from_dict(request.json)
        obj.save()
        return jsonify(obj.to_dict())
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.