1

Javascript

var pageData = new Observable({
    appointmentsList: appointmentsList,
    user_role: ''
});

exports.onLoaded = function (args) {
    page = args.object;
    page.bindingContext = pageData;
    pageData.set('user_role', appSettings.getString('role'));

XML

<Repeater items="{{appointmentsList.booking_dates.bookings}}">
    <Repeater.itemsLayout>
        <StackLayout class="appointment-rows" />
    </Repeater.itemsLayout>
    <Repeater.itemTemplate>
        <StackLayout>
            <WrapLayout visibility="{{ user_role === 'agency' ? 'visible' : 'collapse' }}">
                <Label text="{{user_name}}" class="row-client-name row-client-name-new" />
            </WrapLayout>
        </StackLayout>
    </Repeater.itemTemplate>
 </Repeater>

In above snippet that problem is i am not able to access user_role inside Repeater. Can anybody help me how to access outer element inside Repeater ?

Thanks.

Hardik Vaghani
  • 2,163
  • 24
  • 46

2 Answers2

3

Based on this https://docs.nativescript.org/core-concepts/data-binding#example-4-creating-listview-child-items-based-on-the-itemtemplate, You can try something like this:

<WrapLayout visibility="{{$parents['Repeater'].user_role,$parents['Repeater'].user_role === 'agency' ? 'visibile' : 'collapse' }}">

or this way

<WrapLayout visibility="{{$parents['Page'].user_role,$parents['Page'].user_role === 'agency' ? 'visibile' : 'collapse' }}">
Marek Maszay
  • 1,537
  • 1
  • 9
  • 11
1

You need to use the $parent or $parents[] property.

I believe:

<WrapLayout visibility="{{ $parent.user_role, $parent.user_role === 'agency' ? 'visible' : 'collapse' }}">

Should work. However since this is in a repeater, I don't believe it does anything weird to the parent binding; but if that doesn't work -- you can simply do this: <WrapLayout visibility="{{ $parents['Page'].user_role, $parents['Page'].user_role === 'agency' ? 'visible' : 'collapse' }}">

NativeScript Documentation on this feature: http://docs.nativescript.org/core-concepts/data-binding#binding-to-a-parent-binding-context

Nathanael
  • 5,369
  • 18
  • 23
  • I have `RadListView` in parent of `Repeater `. Does that matter ? – Hardik Vaghani Dec 16 '16 at 07:06
  • 1
    Yes, then you will need to use $parents['Page'] to access it. $parent would only go up one level, to whatever is bound to the RadListView. – Nathanael Dec 16 '16 at 07:22
  • Guess this works fine at getting `user_role` but it is not able to evaluate condition for `$parents['Page'].user_role === 'agency' ? 'visible' : 'collapse'` – Hardik Vaghani Dec 16 '16 at 07:25
  • Did you wrote full way ? `{{ $parents['Page'].user_role, $parents['Page'].user_role === 'agency' ? 'visible' : 'collapse' }}` and not `only {{$parents['Page'].user_role === 'agency' ? 'visible' : 'collapse' }}` – Marek Maszay Dec 16 '16 at 07:27
  • `visibility="{{ $parents['Page'].user_role, $parents['Page'].user_role == 'agency' ? 'visibile' : 'collapse' }}"` – Hardik Vaghani Dec 16 '16 at 07:27
  • **SO SORRY** there was typo in `visibile`. Thanks – Hardik Vaghani Dec 16 '16 at 07:28
  • @MarekMaszay, what does that comma (`,`) syntax mean before and after having data – Sai M. Sep 13 '17 at 10:15
  • https://docs.nativescript.org/core-concepts/data-binding#using-expressions-for-bindings @SaiUnique check this for answer – Marek Maszay Sep 13 '17 at 11:07
  • This doesn't seem to work now checkout https://play.nativescript.org/?template=play-tsc&id=FaTrgV&v=20. – Rakesh Aug 25 '19 at 11:00