Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to access observableArray by index without arr()[index]? #2179

Closed
realYukiko opened this issue Jan 9, 2017 · 4 comments
Closed

Comments

@realYukiko
Copy link

I am sorry I can't find any issue about this problem. If there was, please tell me. Thanks very much : ).

My problem is that I want to get the item from an observableArray by index, and when in the html, just like data-bind=“with:ColumnInfo()[Index]”, the array could not update after having modified the value of array item. I think the reason may be that ColumnInfo()[Index] is not longer an item belongs to ColumnInfo.

I wonder if there is a way to get Item from observableArray by index. Now that foreach can read and change the observableArray, why not index?

@mbest
Copy link
Member

mbest commented Jan 9, 2017

Please provide an example of code that you want to use that isn't working right.

@realYukiko
Copy link
Author

Thanks. @mbest
So it's not recommended to get an item from observableArray by index?

@mbest
Copy link
Member

mbest commented Jan 10, 2017

I don't really understand what problem you are seeing.

@Iiridayn
Copy link

@JSAL- are you asking about a specific index bound in the HTML, such as data-bind="with: ColumnInfo()[3]", and concerned that if index 3 is dropped and re-added into the array, the data-bind wouldn't update, since it was bound to a specific object?

I suspect that the with would catch the observable array access and update when the array changes - nor am I sure I would build my front end with specific index references into an observable array.

That said, (assuming something like with: ColumnInfo()[3]) you could easily implement that as a pureCompued. In your view model, self.nameColumn = ko.pureComputed(function() { return self.ColumnInfo()[3]; }); - however, I'm fairly confident that's essentially exactly what knockout does internally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants