Copied to clipboard

Flag this post as spam?

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


  • Matthieu Nelmes 102 posts 385 karma points
    Nov 10, 2015 @ 16:48
    Matthieu Nelmes
    0

    Creating a Custom Property Editor - Get current content page Id?

    I've created custom sections, list-views, all pulling in data from custom database tables.

    For my next trick, I'm looking to create a Property editor that pulls in/saves data to a custom table with the associated page Id.

    I'm just not sure how to pass the current page id to my Angular functions in order to call the correct data. Forgive me for being such an Angular newb. I'm slowly getting there!

    So, for example I'm envisioning having to specify an angular resource such as:

    angular.module("umbraco.resources")
        .factory("bookingResource", function ($http) {
            return {
                getItemsById: function (id) {
                    return $http.get("backoffice/BookingsAdmin/BookingsApi/GetItemsById?id=" + id);
                }
            };
        });
    

    So this will call the appropriate backoffice APIController. I just can't for the life of me think of how to pass the current page Id there?

  • Nicholas Westby 2043 posts 7020 karma points c-trib
    Nov 10, 2015 @ 17:26
    Nicholas Westby
    101

    It seems like the Umbraco codebase does that by looking at $routeParams.id (and that comes from the URL, I believe): https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Web.UI.Client/src/views/propertyeditors/contentpicker/contentpicker.controller.js#L129

    EDIT: Since this is the accepted answer, I should note that Tim's answer below is probably better. That is, he says to use this instead:

    editorState.current.id
    

    I suspect that is better because it is more likely to work in something like the dialog that appears when you click "Edit" under a picked node in a multinode treepicker.

  • Matthieu Nelmes 102 posts 385 karma points
    Nov 10, 2015 @ 18:20
    Matthieu Nelmes
    0

    Of course!!! Fantastic!

    Although I think the section you've referenced refers to the starting NodeId of a content picker. But still, there should be something here I can use.

    Thanks!

  • Tim Miller 32 posts 252 karma points
    Nov 10, 2015 @ 17:55
    Tim Miller
    2

    See this thread: https://our.umbraco.org/forum/umbraco-7/developing-umbraco-7-packages/48154-How-to-get-current-ID-within-v7-Property-Editor

    In short, this should probably do the trick for you though:

    editorState.current.id
    

    Don't forget to include editorState in your call.

  • Nicholas Westby 2043 posts 7020 karma points c-trib
    Nov 10, 2015 @ 18:41
    Nicholas Westby
    1

    Interesting. I wonder if editorState.current.id is preferable to $routeParams.id. I think it might be, such as in the case that a page is opened for editing in a dialog (i.e., the URL wouldn't necessarily change, but the editor state for that dialog may). This is possible, for example, from the multinode treepicker when the experiment edit mode is enabled.

    Here is some documentation on editorState: http://umbraco.github.io/Belle/#/api/umbraco.services.editorState

  • Matthieu Nelmes 102 posts 385 karma points
    Nov 11, 2015 @ 16:51
    Matthieu Nelmes
    0

    Just to report back,

    editorState.current.id
    

    did the trick!

Please Sign in or register to post replies

Write your reply to:

Draft