Last active
September 3, 2015 20:58
-
-
Save jbnv/b04aa26e02e8b1585214 to your computer and use it in GitHub Desktop.
Example of publish and subscribe in Durandal. From Durandal's HTML Samples download.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<section> | |
<div class="col-md-6" data-bind="compose: publisher"></div> | |
<div class="col-md-6" data-bind="compose: subscriber"></div> | |
</section> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['./publisher', './subscriber'], function (publisher, subscriber) { | |
return { | |
publisher:publisher, | |
subscriber: subscriber | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<h1>Publisher</h1> | |
<form class="form-inline" role="form"> | |
<div class="form-group"> | |
<input type="text" class="form-control" placeholder="message" data-bind="value: message, valueUpdate: 'afterkeydown'"> | |
</div> | |
<button type="submit" class="btn btn-default" data-bind="click: publish, enable: canPublish">Publish</button> | |
</form> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['durandal/app', 'knockout'], function (app, ko) { | |
var message = ko.observable(); | |
var canPublish = ko.computed(function () { | |
return message() ? true : false; | |
}); | |
return { | |
message: message, | |
canPublish:canPublish, | |
publish: function () { | |
app.trigger('sample:event', message()); | |
} | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<h1> | |
Subscriber | |
<button class="btn btn-success" data-bind="enable: !subscription(), click: subscribe">Subscribe</button> | |
<button class="btn btn-danger" data-bind="enable: subscription, click: unsubscribe">Unsubscribe</button> | |
</h1> | |
<ul data-bind="foreach: received"> | |
<li data-bind="text: $data"></li> | |
</ul> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['durandal/app', 'knockout'], function (app, ko) { | |
return { | |
received: ko.observableArray([]), | |
subscription:ko.observable(), | |
subscribe: function () { | |
var sub = app.on('sample:event').then(function(message) { | |
this.received.push(message); | |
}, this); | |
this.subscription(sub); | |
}, | |
unsubscribe: function () { | |
this.subscription().off(); | |
this.subscription(null); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment