Lesson 7: Create a custom Dojo widget
In this lesson, you create a custom Dojo widget so that
users can rate movies and add comments.
About this task
- An HTML file
- A JavaScript file
- A CSS file
Procedure
Results
RateAndComment.js
RateAndComment.js now
looks similar to the following code snippet:
define("widgets/RateAndComment", [ "dojo", "dijit", "dijit/_Widget",
"dijit/_TemplatedMixin", "dijit/_WidgetsInTemplateMixin",
"dojo/text!widgets/templates/RateAndComment.html", "dojox/form/Rating",
"dojo/data/ItemFileWriteStore", "dijit/form/Textarea", "dijit/Dialog",
"dijit/form/Button", "dijit/TitlePane"
], function(dojo, dijit, _Widget, _TemplatedMixin, _WidgetsInTemplateMixin) {
dojo.declare("widgets.RateAndComment", [ dijit._Widget,
dijit._TemplatedMixin, dijit._WidgetsInTemplateMixin ],
{
// Path to the template
templateString : dojo.cache("widgets",
"templates/RateAndComment.html"),
movieTitle : "",
rcStore : new dojo.data.ItemFileWriteStore({
data : {
items : []
}
}),
// Override this method to perform custom behavior during dijit
// construction.
// Common operations for constructor:
// 1) Initialize non-primitive types (i.e. objects and arrays)
// 2) Add additional properties needed by succeeding lifecycle
// methods
constructor : function() {
},
getComment : function() {
return this.comments.get('value');
},
getRating : function() {
return this.rating.get('value');
},
setComment : function(value) {
this.comments.set("value", value);
},
setRating : function(value) {
this.rating.set("value", value);
},
showFeedbackTools : function(movieTitle) {
this.movieTitle = movieTitle;
this.dialogbox.show();
},
saveFeedback : function() {
var userRating = this.getRating();
var userComment = this.getComment();
this.rcStore.newItem({
title : this.movieTitle,
comment : userComment,
rating : userRating
});
dojo.create("li", {
innerHTML : "<b>Title:</b> " + this.movieTitle + " <b>Rating:</b>"
+ userRating + "<br> <b>Comments:</b> " + userComment
}, this.feedbacklist);
this.dialogbox.hide();
this.setComment("");
this.setRating(0);
},
getCommentsByMovieTitle : function(movieTitle) {
var comments = [];
var store = this.rcStore;
this.rcStore.fetch({
query : {
title : movieTitle
},
onComplete : function(items, request) {
dojo.forEach(items, function(item) {
comments.push(store.getValue(item, "comment"));
});
}
});
return comments;
},
// When this method is called, all variables inherited from
// superclasses are 'mixed in'.
// Common operations for postMixInProperties
// 1) Modify or assign values for widget property variables
// defined in the template HTML file
postMixInProperties : function() {
},
// postCreate() is called after buildRendering(). This is useful
// to override when
// you need to access and/or manipulate DOM nodes included with
// your widget.
// DOM nodes and widgets with the dojoAttachPoint attribute
// specified can now be directly
// accessed as fields on "this".
// Common operations for postCreate
// 1) Access and manipulate DOM nodes created in
// buildRendering()
// 2) Add new DOM nodes or widgets
postCreate : function() {
}
});
});
RateAndComment.html
In the Source
view, RateAndComment.html now looks similar to the
following code snippet:
In the Design view, the custom
widget now looks similar to the following screen capture:<div class="RateAndComment">
<div data-dojo-type="dijit.Dialog" data-dojo-attach-point="dialogbox"
data-dojo-props="title: 'Rate and Comment!'">
Rate: <div data-dojo-type="dojox.form.Rating" data-dojo-attach-point = "rating" data-dojo-props = "numStars: '5'"></div>
<br>
<br>
Comments: <div data-dojo-type="dijit.form.Textarea" data-dojo-attach-point = "comments" style="width: 60%;"></div>
<br>
<button data-dojo-type="dijit.form.Button" data-dojo-attach-event="onClick: saveFeedback">Submit</button>
</div>
<div data-dojo-type="dijit.TitlePane" data-dojo-props="title: 'History of Ratings and Comments'">
<ol data-dojo-attach-point="feedbacklist"></ol>
</div>
</div>

Lesson checkpoint
You created a custom Dojo widget.
You learned:
- How to modify the HTML template for a custom Dojo widget
- How to modify the JavaScript file for a custom Dojo widget







