Copied to clipboard

Flag this post as spam?

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


  • vcamargo 27 posts 98 karma points
    Feb 27, 2016 @ 20:47
    vcamargo
    0

    How can I access the value of an AngularJS model within a macro?

    I tried to set isParameterEditor to true in the package.manifest and access it like this:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{ var selection = CurrentPage.Children.Where("Visible").OrderBy("CreateDate desc"); }
    
    <ul>
      @foreach (var item in selection)
      {
        <li>@item.NameOfThePropertyEditor.NameOfTheModelOne</li>
        <li>@item.NameOfThePropertyEditor.NameOfTheModelTwo</li>
        <li>@item.NameOfThePropertyEditor.NameOfTheModelThree</li>
        <li>@item.NameOfThePropertyEditor.NameOfTheModelFour</li>
      }
    </ul>
    

    But keep receiving an "Error loading Partial View script".

    Any ideias of what I've been doing wrong?

  • Marc Goodson 2157 posts 14411 karma points MVP 9x c-trib
    Feb 27, 2016 @ 23:19
    Marc Goodson
    0

    Hi Vinicius

    Are you trying to read the value of a parameter that has been set on your macro ?

    If so you can read the value of a parameter set on a macro in razor like so:

    var parameterValue = Model.GetParameterValue

    (setting isParameterEditor allows you to use the property editor as a macro parameter type)

    or

    Are you just trying to read the value of a property on the document type as you loop through a selection of nodes ? (and the property is a custom editor that stores complex values as Json ?)

    In this case (isParameterEditor is irrelevant), you would need to first retrieve the value of the property, and then deserialise the Json to be able to access the values saved on the angular model.

    eg I think something like this would work:

    var JsonPropertyData = item.propertyAlias; dynamic propertyData = Json.Decode(jsonPropertyData);

    @propertyData.NameOfTheModelOne

  • vcamargo 27 posts 98 karma points
    Feb 29, 2016 @ 17:47
    vcamargo
    0

    Hi, Marc! Thanks for taking the time to reply. I think that my case would be the second one. But I tried your code and couldn't solve my problem.

    Do you have any other idea?

    I'll try to explain it better: It is a custom property editor that I'm trying to retrieve its values.

  • Marc Goodson 2157 posts 14411 karma points MVP 9x c-trib
    Feb 29, 2016 @ 18:26
    Marc Goodson
    0

    Hi Vinicius

    Ok, then the first thing to confirm is currently what is the value of:

    @item.propertyAlias

    for your custom property when you loop through

    is it a JSON object ?

    regards

    Marc

  • vcamargo 27 posts 98 karma points
    Feb 29, 2016 @ 18:46
    vcamargo
    0

    Would you mind to explain me how can I get the property type?

  • Marc Goodson 2157 posts 14411 karma points MVP 9x c-trib
    Feb 29, 2016 @ 21:53
    Marc Goodson
    0

    When you added your custom property to your document type, what alias did you give it ?

    and what do you see if you just write the property out ?

    So for example in the Fanoe Starter kit the blog overview template loops through each blog post to give a teaser for each post:

     @foreach (var post in CurrentPage.Children)
                    {
                        <div class="col-sm-6">
                            <div class="content equal">
                                <a href="@post.Url">
                                    <div class="date">@post.CreateDate.ToLongDateString()</div>
                                    <h2>@post.Name</h2>
                                    @post.content
                                    <p>@Umbraco.Truncate(post.Introduction, 240, true)</p>
                                </a>
                            </div>
                        </div>
                    }
    

    However the property 'content' is one I've added here under the h2 element, it's a grid property and so it's value is stored as Json

    By writing it out here in the template, I can see what value has been stored in Umbraco for the property:

    and it comes out a bit like this:

    enter image description here

    eg the bits in red - lots of Json !!!

    So now if we try the Json.Decode trick

     @foreach (var post in CurrentPage.Children)
                    {
                        dynamic jsonContent = Json.Decode(post.content.ToString());
                        <div class="col-sm-6">
                            <div class="content equal">
                                <a href="@post.Url">
                                    <div class="date">@post.CreateDate.ToLongDateString()</div>
                                    <h2>@post.Name</h2>
                                    @jsonContent.name
                                    <p>@Umbraco.Truncate(post.Introduction, 240, true)</p>
                                </a>
                            </div>
                        </div>
                    }
    

    you can see I've created a dynamic variable called 'jsonContent' and used Json.Decode to parse the blob of Json for the content property into a dynamic object.

    now instead of writing out the blog of json with @post.content, I can instead use @jsonContent.name or write out any of the properties in the json blog using the dynamic syntax, with the result...

    enter image description here

  • vcamargo 27 posts 98 karma points
    Mar 03, 2016 @ 19:58
    vcamargo
    0

    I tried your code but couldn't make it work. Could you point me to the part of the documentation about this issue? As I couldn't find it by myself.

    I'm trying to display a custom html table in the backoffice that will be rendered with its css within a macro as a product. But I'm not sure if I'm taking the right approach.

Please Sign in or register to post replies

Write your reply to:

Draft