scottdurow / SparkleXrm

An open-source library for building Dynamics CRM XRM solutions using Script#, jQuery & Knockoutjs.
MIT License
264 stars 194 forks source link

ObservableArray binding #87

Closed thdbased closed 8 years ago

thdbased commented 8 years ago

I'm am having serious troubles getting this to work.

I'm getting JSON data from a remote server, I made and ObservableArray for the items inside the Json (which I parsed). Everything is ok for this. I checked using browser console and my data is valid.

Now I'm trying to foreach this array in the UI using

<td data-bind="foreach: this.Data">
<h3 data-bind="text: properties.Title"></h3> //properties.Title are object in my Array
</td>

This just won't work, I tried numerous combinations. Is there a problem with the fact that I am doing this on initial load? I am populating my ObservableArray in the constructor.

Any help, please? Would be very thankful if someone would help me out.

scottdurow commented 8 years ago

I suspect the issue is the use of 'this' in your foreach binding.

If 'Data' is an ObservableArray on your View Model (with 'PreserveCase' so that it doesn't become camel case in the script) - you would simply use data-bind="foreach: Data"

For an example of this see - the html that renders a template foreach contact - https://github.com/scottdurow/SparkleXrm/blob/ebd4a3579cf656fc38a26119f781fbe781f0c9ec/SparkleXrmSource/DebugWeb/WebResources/dev1_/html/ContactCardViewer.htm#L67

This bound to the 'Contacts' field - https://github.com/scottdurow/SparkleXrm/blob/ebd4a3579cf656fc38a26119f781fbe781f0c9ec/SparkleXrmSource/Client/InlineSubGrids/ViewModels/ContactCardViewModel.cs#L15

Notice that since [PreserveCase] is not used, the script name is camelCase.

You can read more about the binding context at http://knockoutjs.com/documentation/binding-context.html

Hope this helps!

thdbased commented 8 years ago

Oh dear lord. It was indeed the "this".

Thank you very much :)