javascript - Durandal compose change value of a variable of bound viewmodel -
in app, have section called 'notifications' inside view, called 'home.html'
. need use section in view, copied notifications markup inside 'notifications.html', i'm using through composition.
the problem don't want copy code use notifications work 'home.js' viewmodel of new view going use notifications. instead of having observablearray
of notifications inside view viewmodel, want have module 'notification.html' contains data. don't know how can change value of observablearray
inside 'notification.js' other viewmodels.
here did far:
notification.js
define(['knockout'], function(ko) { var notifications = ko.observablearray([]), setnotifications = function(data) { notifications(data) } return { notifications: notifications, setnotifications: setnotifications }
home.js
define(['knockout', 'notification'], function(ko, notification) { var self = ... self.activate = function() { http.get('...').then(function(data) { notification.setnotification(data) }) }
home.html
<div data-bind="compose: 'viewmodels/notifications'"></div>
what know when using viewmodel path in compose binding, markup of viewmodel injected , bound it's module viewmodel.
the problem: want change value of 'notifications' array inside 'notification.js'
viewmodel of main view, 'home.js'
. know current tentative isn't going work, because believe instance of notifications.js
viewmodel being injected notifications.html
isn't same instance require inside home.js
. how can achieve that?
i solve problem using model
property on compose
binding.
markup:
<div data-bind="compose: { view: 'views/notifications', model: $root.notificationmodel }"></div>
notification.js has same code - returns viewmodel ("return vm")
and in home.js file:
define(['knockout', 'plugins/http', 'notification'], function(ko, http, notificationmodel) { var self = self.notificationmodel; ... self.activate = function() { return http.get('').then(function(data) { self.notificationmodel = new notificationmodel() self.notificationmodel.setnotifications(data) }) } })
Comments
Post a Comment