Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • jacob phillips 130 posts 372 karma points
    Jan 16, 2015 @ 06:53
    jacob phillips
    0

    Get the current node into a service

    Hi,

    I am going through the various Angular/WebApi examples for creating Umbraco service...and watching some Angular tutorials...getting there...

    So I got this example working.. https://github.com/perploug/UkFest-AngularJS-Demo

    And I have moved onto this one which demonstrates CRUD operations: https://github.com/TimGeyssens/UmbracoAngularBackofficePages

    Could someone more experienced tell me...

    If I create a service that does something in relation to the document that it runs on...how can I pass in the node id of this document?

    So for example, consider in "UmbracoAngularBackofficePages" project, in PersonApiController.cs we got:

    public Person GetById(int id)
    {
     var query = new Sql().Select("*").From("people").Where<Person>(x => x.Id == id);
     return DatabaseContext.Database.Fetch<Person>(query).FirstOrDefault();
    
    }
    

    and then in person.resource.js we got:

    getById: function (id) {
       return $http.get("backoffice/Example/PersonApi/GetById?id=" + id);
    },
    

    In the first example I went through, there is a personpicker.controller.js file:

    angular.module("umbraco")
        .controller("My.PersonPickerController", function($scope, personResource){
            personResource.getAll().then(function(response){
                $scope.people = response.data;
            });
        });
    

    So instead of calling getAll(), if I wanted to get getById(with node id that this is running on), how can I make it so that whatever node I put this service on, it use the current id?

    I realize it might not make sense why I would want to do exactly that, but I'm using these examples as a recipe to do something else, and I need to start off by passing in this current id. I can't see how to do it.

  • jacob phillips 130 posts 372 karma points
    Jan 16, 2015 @ 16:59
    jacob phillips
    0

    Hmmm, I guess maybe in my .cs controller, in the method I want, I could just use umbraco node factory..something like...

    Node node = Node.GetCurrent();
    var query = new Sql().Select("*").From("myTable").Where<MyCustomObject>(x => x.Id == node.Id);
    

    So,. it's not really about getting the context from the Angular at all..?

Please Sign in or register to post replies

Write your reply to:

Draft