Monday, May 19, 2014
Monday, October 21, 2013
Ganelon tutorial, part 2: widgets and actions.
If you want to see the final result of an entire tutorial, you can view the tutorial app running live at http://ganelon-tutorial.tomeklipski.com or you can check out/star/fork the source code at http://github.com/tlipski/ganelon-tutorial.
For more information regarding Ganelon, you can visit the project website: http://ganelon.tomeklipski.com.
Connecting to MongoDB
Using mongodb is Clojure project is very easy and there's nothing Ganelon-specific about it.
First we will add dependency for congomongo library ([congomongo "0.4.1"]) to project.clj:
:dependencies [[ganelon "0.9.0"]
[congomongo "0.4.1"]
[crypto-random "1.1.0"]]
There's also another new library referenced - crypto-random. It is a useful tool when it comes to generating secure random identifiers - something that will come in very handy when creating new meetups or invitations.
Having the MongoDB library in place, we can add connection setup to src/ganelon/tutorial.clj:
(ns ganelon.tutorial
(:gen-class)
(:require [ganelon.tutorial.pages.routes]
[ring.middleware.stacktrace]
[ring.middleware.reload]
[ganelon.web.middleware :as middleware]
[ganelon.web.app :as webapp]
[noir.session :as sess]
[somnium.congomongo :as db]))
(defn get-mongo-url []
(or
(get (System/getenv) "MONGOHQ_URL")
(System/getProperty "MONGOHQ_URL")
"mongodb://localhost/meetups"))
(defn initialize[]
(db/set-connection! (db/make-connection (get-mongo-url))))
The get-mongo-url function returns mongo db url either from environment or from default development setup. In real-life scenario, the connection options will be much more complex, including username&password, failover options, etc.
It is also noteworthy, that we don't have to manage MongoDB connections ourselves - the underlying libraries are taking care of that for us.
The initialize function is referenced in lein-ring plugin configuration and will be invoked upon ring application initialization.
Accessing the data with MongoDB
To separate our application from underlying persistence layer, we will create seperate service namespaces for basic data integration. Fortunately, most of the work is done by congomongo library and we can use native Clojure data structures in all layers.
Data operations are defined in three namespaces:
- src/ganelon/tutorial/services/meetup.clj - meetup entity management.
- src/ganelon/tutorial/services/meetup_time.clj - possible times for each meetup.
- src/ganelon/tutorial/services/invitation.clj - meetup invitations.
All of functions in these namespace are quite simple and mostly cover congomongo (somnium.congomongo is imported as db) references, for example:
(defn retrieve-list [meetup-id]
(db/fetch :meetup-times :where {:meetup-id meetup-id}
:sort {:date 1 :time 1}))
(defn add-time! [meetup-id date time]
(when (empty? (db/fetch :meetup-times :where
{:meetup-id meetup-id :date date :time time}))
(db/insert! :meetup-times {:meetup-id meetup-id :date date
:time time :create-time (java.util.Date.)
:accepted []})))
Defining widgets
Finally, having all the other components in place, we are able to create some Ganelon-specific widgets and actions!
New meetup widget
The new meetup widget consists of two parts, listed below. Entire definition is contained in ganelon.tutorial.widgets.meetup-add namespace.
First of all, we have widget definition. As you can see below, it is a standard Clojure function, and it is invoked explicitly from Clojure code - ganelon.tutorial.pages.routes/meetup-layout to be exact.
(defn new-meetup-widget []
(widgets/with-div
[:h1 "New meetup"]
[:p "Please provide meetup details:"]
(widgets/action-form "meetup-create" {} {:class "form well"}
[:div.control-group [:label.control-label {:for "inputTitle"} "Title"]
[:div.controls
[:input#inputTitle {:placeholder "Title for a meetup" :type "text"
:name "title"
:required "1"}]]]
[:div.control-group [:label.control-label {:for "inputPlace"} "Place"]
[:div.controls
[:input#inputPlace {:placeholder "Place for a meetup" :type "text"
:name "place"
:required "1"}]]]
[:div.control-group
[:div.controls
[:button.btn.btn-primary.btn-large {:type "submit"} "Create"]]])))
We use Hiccup to generate HTML form, styled by Bootstrap. One significant difference is that we don't use [:form] HTML tag, but rather ganelon.web.widgets/action-form function. This function allows us to reference a Ganelon action to be invoked upon form submission - client side.
Action meetup-create, creating new meetup is defined with a help of a ganelon.web.actions/defjsonaction macro.
(actions/defjsonaction "meetup-create" [title place]
(let [id (meetup/create! title place)]
[(ui-operations/open-page (str "/meetup/edit/" id))]))
The action itself is pretty straightforward:
- First we create new meetup in MongoDB using form params - the destructuring syntax underneath is Compojure's, since we are using its handler infrastructure.
- Then, we return one operation to be performed - to redirect the browser to meetup's edit page, containing its random and unique id.
In the next part of the tutorial, this action will change and will not reload entire page - and we will use custom JavaScript actions to achieve that.
Meetup details widget (and sub-widgets)
Meetup details widget is more complicated than the previous one, since it has to support not only edition of meetup place and title, but also:
- Allow the user to manage possible times for a meetup with response statuses, which is implemented in
ganelon.tutorial.widgets.meetup-timesnamespace. - Manage meetup invitations - in
ganelon.tutorial.widgets.meetup-invitationsnamespace.
Please observe, that the main meetup details widget is referencing sub-widgets as functions: meetup-edit-form-widget, meetup-times/meetup-times-widget and meetup-invitations/meetup-invitations-widget. This way, we can decompose our web application into a set of widgets with clearly defined dependencies.
(defn meetup-details-widget [meetup-id]
(widgets/with-div
(let [meetup (meetup/retrieve meetup-id)]
[:div
[:div {:style "padding-top: 20px;"}
(meetup-edit-form-widget meetup)]
(meetup-times/meetup-times-widget meetup-id)
(meetup-invitations/meetup-invitations-widget meetup-id)])))
As we have more possibilities here than just meetup creation, we can make the form more dynamic. For example, we will capture :onchange JavaScript events for meetup title and place to update the fields in MongoDB without save button. Even more, we will display a confirmation label next to a saved field.
In meetup edition widget definition, we will add standard HTML attributes, referencing our Ganelon action (meetup-title-update) - but client-side! As Ganelon has information about defined actions, there is an interface available as a standard Compojure route (by default under /ganelon/actions.js), publishing all these functions to be referenced in JavaScript client-side:
(defn meetup-edit-form-widget [meetup]
(widgets/with-div
[:h1 "Meetup details"]
(let [url (str (web-helpers/current-request-host-part) "/meetup/edit/"
(:_id meetup))]
[:p "Meetup admin url: " [:a {:href url } url]])
[:form.form-horizontal.well
[:div.control-group [:label.control-label {:for "inputTitle"} "Title"]
[:div.controls [:input#inputTitle.input-xlarge
{:placeholder "Title for a meetup" :type "text"
:value (:title meetup)
:onkeypress "$('#update-title-loader > *').fadeOut();"
:onchange (str "GanelonAction.meetup_title_update('"
(:_id meetup)
"', this.value);")
:name "title"
:required "1"}]
[:span#update-title-loader]]]
[:div.control-group [:label.control-label {:for "inputPlace"} "Place"]
[:div.controls [:input#inputPlace.input-xlarge
{:placeholder "Place for a meetup" :type "text"
:value (:place meetup)
:onkeypress "$('#update-place-loader > *').fadeOut();"
:onchange (str "GanelonAction.meetup_place_update('"
(:_id meetup)
"', this.value);")
:name "place"
:required "1"}]
[:span#update-place-loader]]]]))
Once more, we are using standard Bootstrap 2 classes here to render a form.
Actions update meetup details in MongoDB, but also update form elements with jQuery's fade effect, provided by ganelon.web.ui-operations/fade function - for example:
(actions/defjsonaction "meetup-title-update" [id title]
(meetup/update! id :title title)
[(ui-operations/fade (str ".meetup-title-" id)
(hiccup.util/escape-html title))
(ui-operations/fade "#update-title-loader"
(hiccup.core/html [:span {:onmouseover "$(this).fadeOut();"}
" " [:i.icon-check] " Saved"]))])
You are welcome to browse the operations available in ganelon.web.ui-operations namespace - but please note, that it is also very easy to define your own, more fitting for you apps specific needs!
Meetup times widget allows us to add new invitations and to list and manage existing ones - including invitation confirmation status for each time and invitation. The whole implementation is available in ganelon.tutorial.widgets.meetup-times namespace.
(defn toggle-meetup-times-button [t inv editable-invitation-ids]
(let [accepted? (some #{(:_id inv)} (:accepted t))]
(widgets/with-div
[:div {:style (str "padding: 8px; " (if accepted?
"background-color: #5bb75b"
"background-color: #9da0a4"))}
(if (or (not editable-invitation-ids) (some #{(:_id inv)}
editable-invitation-ids))
(widgets/action-loader-link "meetup-times-toggle-invitation"
{:invitation-id (:_id inv)
:id (:_id t)
:value (not accepted?)} {}
(if accepted? [:i.icon-thumbs-up ] [:i.icon-thumbs-down ]))
" ")])))
(defn meetup-times-list-widget [meetup-id editable-invitation-ids]
(widgets/with-widget "meetup-times-list-widget"
(let [times (meetup-time/retrieve-list meetup-id)]
(if (not-empty times)
(let [invitations (invitation/retrieve-list meetup-id)]
[:table.table.table-striped.table-hover {:style "width: initial"}
[:thead [:tr
(when-not editable-invitation-ids [:th ])
[:th "Date"] [:th "Time"]
(for [inv invitations]
[:th [:small
(hiccup.util/escape-html (:name inv))]])]]
(for [t times]
[:tr (when-not editable-invitation-ids
[:td (widgets/action-link "meetup-remove-time"
{:id (:_id t)} {} [:i.icon-remove ])])
[:td (:date t)] [:td (:time t)]
(for [inv invitations]
[:td {:style "text-align: center; padding:0px;
border-left: 1px solid #ccc"}
(toggle-meetup-times-button t
inv editable-invitation-ids)])])])
[:div.alert [:i "No meetup times defined yet!"]]))))
Please observe, that we are using widgets in loop here - as ganelon.web.widgets/with-div macro binds widget each time to a unique and random value, we can tons of instances of the same widget side by side.
We can also define a function which will return Ganelon UI operation refreshing times list - to be invoked externally (e.g. from invitations list widget):
(defn refresh-meetup-times-list-widget-operations [meetup-id]
(ui-operations/fade "#meetup-times-list-widget"
(meetup-times-list-widget meetup-id nil)))
This operation references widget by id attribute, allowing us to invoke it without widget-id context set.
The meetup-times-toggle-invitation action takes advantage of Compojure's route params support, allowing us to access them by name directly:
(actions/defwidgetaction "meetup-times-toggle-invitation"
[id invitation-id value]
(if (boolean (Boolean. value))
(meetup-time/accept-time! id invitation-id)
(meetup-time/reject-time! id invitation-id))
(toggle-meetup-times-button (meetup-time/retrieve id)
(invitation/retrieve invitation-id) [invitation-id]))
meetup-add-time action itself provides simple validation mechanism:
(actions/defjsonaction "meetup-add-time" [id date time]
(if-let [new-mu (meetup-time/add-time! id date time)]
;success
[(ui-operations/make-empty "#meetup-add-time-message")
(ui-operations/fade "#meetup-times-list-widget"
(meetup-times-list-widget id nil))]
;error - such time already exists
(ui-operations/fade "#meetup-add-time-message"
(hiccup.core/html
[:div.alert
[:button.close {:type "button" :data-dismiss "alert"} "×"]
[:p "Such date & time combination already exists!" ]]))))
ganelon.web.ui-operations/make-empty operation simply removes all contents from designated dom element.
Meetup invitations list widget works on similar principle as meetup times widget listed above:
(defn meetup-invitations-widget [meetup-id]
(widgets/with-widget "meetup-invitations-widget"
[:h2 "Meeting invitations"]
(widgets/action-form "invitation-create"
{:meetup-id meetup-id}
{:class "form-horizontal well"}
[:span "Recipient's name:"] " "
[:input {:type "text" :name "name" :required "1"}] " "
[:button.btn.btn-primary "Create new invitation"]
)
(let [invitations (invitation/retrieve-list meetup-id)]
(if (empty? invitations)
[:div.alert
"No invitations created yet. Please use the form above to add some!"]
(for [inv invitations]
[:p
[:b (hiccup.util/escape-html (:name inv))] [:br]
"Link: " [:a {:href
(str (web-helpers/current-request-host-part)
"/i/" (:_id inv))}
(str (web-helpers/current-request-host-part)
"/i/" (:_id inv))]
(widgets/action-link "invitation-cancel"
{:meetup-id meetup-id :id (:_id inv)}
{:class "pull-right"}
[:i.icon-remove] " Cancel")])))))
(actions/defwidgetaction "invitation-create" [name meetup-id]
(invitation/create! meetup-id name)
(actions/put-operation!
(meetup-times/refresh-meetup-times-list-widget-operations
meetup-id))
(meetup-invitations-widget meetup-id))
(actions/defwidgetaction "invitation-cancel" [meetup-id id]
(invitation/delete! id)
(actions/put-operation!
(meetup-times/refresh-meetup-times-list-widget-operations
meetup-id))
(meetup-invitations-widget meetup-id))
One thing worth noticing is that we put operation refreshing meetup times widget upon invitation creation or deletion. Instead of accessing widget functions for meetup times directly, we call a wrapper function which encapsulates all the necessary logic - e.g. DIV identification and returns ready to use Ganelon UI operation.
Invitation widget
Invitation widget is used by meetup attendees and allows them only to confirm or reject their presence at the meetup at given time. Whole implementation is provided withing ganelon.tutorial.widgets.invitation-details namespace.
(defn invitation-details-widget [id]
(widgets/with-div
(if-let [inv (invitation/retrieve id)]
(let [meetup (meetup/retrieve (:meetup-id inv))]
[:div
[:div [:h2 (hiccup.util/escape-html (:title meetup))]
[:p "Located at: " [:b (hiccup.util/escape-html (:place meetup))]]
[:p "Sent to: " [:b (hiccup.util/escape-html (:name inv))]]]
[:h2 "Confirm your presence"]
[:p "Please mark times & dates that are best for you:"]
(meetup-times/meetup-times-list-widget (:meetup-id inv) [id])])
[:div.alert.alert-error [:h2 "Invitation not found"]
[:p "Invitation with a supplied id has not been found.
Please make sure that the link is correct."]])))
Please note, that we are re-using meetup-times/meetup-times-list-widget to provide meetup times and to allow the attendee to confirm or negate his/her presence at the meetup. We do so by providing additional parameter to function invocation, limiting displayed invitations only to the currently used.
Summary
In this blog post we have learned, that it is very easy to build dynamic, AJAX-oriented web application entirely in Clojure, server side. Even better, such approach allows us to re-use existing components in different scenarios. We don't have to stick with our application being single-page and we are totally 100% SEO friendly in such approach, as widgets are used both in standard HTML page rendering as in updates to the page itself.
In the next post I will show how to add middleware for security handling and how to define custom Ganelon UI operations in JavaScript.
Tuesday, August 20, 2013
Ganelon tutorial, part 1: basic setup, routes & templates
If you want to see the final result of an entire tutorial, you can view the tutorial app running live at http://ganelon-tutorial.tomeklipski.com or you can check out/star/fork the source code at http://github.com/tlipski/ganelon-tutorial.
For more information regarding Ganelon, you can visit the project website: http://ganelon.tomeklipski.com.
Creating Ganelon project
lein new to start a new project or set up the project.clj file and src/, script/ and resources/ directories manually.In either way, it is important that the project.clj file contains a dependency on ganelon 0.9.0 library. What is worth mentioning, is that ganelon comes with dependencies on lib-noir, Compojure & hiccup, so in simple cases, it is enough to just add this one dependency when it comes to Clojure web apps.
Ganelon 0.9.0 is deployed to clojars, so it will get fetched alongside other Clojure libraries.
Our project.clj at this moment looks like this:
(defproject ganelon-tutorial "0.9-SNAPSHOT"
:description "Ganelon tutorial"
:url "http://ganelon.tomeklipski.com"
:dependencies [[ganelon "0.9.0"]
[org.clojure/clojure "1.5.1"]]
:license {:name "Eclipse Public License - v 1.0"
:url "http://www.eclipse.org/legal/epl-v10.html"
:distribution :repo
:comments "same as Clojure"}
:plugins [[lein-ring "0.8.6"]]
:ring {:handler ganelon.tutorial/handler :init ganelon.tutorial/initialize})
As we can see, there is only one dependency other than Clojure 1.5.1 - it is for ganelon 0.9.0. There is also a lein-ring configuration with a pretty standard settings - an initialize function, which at this moment does nothing but will establish a connection pool for MongoDB and a handler reference.
Handler definition
Our handler will be using Ganelon's simple mechanism for establishing routes - and we will mix app-handler from Ganelon with standard Ring middleware.
You can add your own handlers and middleware here and it will just work. If you don't want to or cannot use Compojure routes, you can just skip this step altogether and use raw Ring handlers from ganelon.web.actions package.
(def handler
(->
(ganelon.web.app/app-handler
(ganelon.web.app/javascript-actions-route))
middleware/wrap-x-forwarded-for
(ring.middleware.stacktrace/wrap-stacktrace)
(ring.middleware.reload/wrap-reload {:dirs ["src/ganelon/tutorial/pages"]})))
View the entire ganelon/tutorial.clj file.
Please also note, that we are using a convienent ring.middleware.reload/wrap-reload middleware, which will reload all the changes to Clojure files in specified directories - src/ganelon/tutorial/pages in this case.
Defining the Hiccup templates
The simplest way (but sometimes not the most effective) to achieve common layout and HTML code for pages with Hiccup is to define them as functions, accepting page content as parameters:
(defn layout [& content]
(hiccup/html5
[:head [:meta {:name "viewport" :content "width=device-width, initial-scale=1.0"}]
[:title "Ganelon tutorial - Meetups"]
;real life site should use CDN/minify to serve static resources
(hiccup/include-css "/ganelon/css/bootstrap.css")
(hiccup/include-css "/ganelon/css/bootstrap-responsive.css")
]
[:body.default-body [:div#navbar (navbar)]
[:div.container {:style "padding-top: 70px"}
content]
[:footer {:style "opacity:0.9; text-align: center; padding: 30px 0; margin-top: 70px; border-top: 1px solid #E5E5E5; color: #f6f6f6; background-color: #161616;"}
[:div.container [:p "The Ganelon framework has been designed, created and is maintained by " [:a {:href "http://twitter.com/tomeklipski"} "@tomeklipski"] "."]
[:p "The code is available under " [:a {:href "http://opensource.org/licenses/eclipse-1.0.php"} "Eclipse Public License 1.0"] "."]
[:p [:a {:href "http://github.com/tlipski/ganelon-tutorial"} "View the sources on GitHub."]]
[:p "This interactive tutorial runs on " [:a {:href "http://cloudbees.com"} "CloudBees"]
" and " [:a {:href "http://mongohq.com"} "MongoHQ"] "."]
]]]))
View the entire ganelon/tutorial/pages/common.clj file.
As you can see, we are using standard Bootstrap2 CSS/classes - nothing fancy.
We have also defined a helper layout with common UI components and put it routes.clj file:
(defn meetup-layout [& contents]
(common/layout
[:div.row-fluid [:div.span3 [:div {:style "border: 1px dashed #363636"} "TODO - new meetup widget here"]
[:div {:style "border: 1px dashed #363636"} "TODO - meetup list widget here"]]
[:div.span1 ]
[:div.span8 [:div#contents contents]]]))
Defining routes
With templates already defined, we can set up our routes. They will contain placeholders for widgets, as these will be added in the next steps of the tutorial:
(dyna-routes/defpage "/" []
(meetup-layout
[:div.hero-unit [:h1 "Welcome"]
[:p "Welcome to the interactive tutorial for " [:a {:href "http://ganelon.tomeklipski.com"} "Ganelon micro-framework."]]
[:p "This sample application used to manage meetups provides links to display source of every widget and action used.
In addition to that, each widget has a dashed border to mark its boundary."]]))
(dyna-routes/defpage "/meetup/edit/:id" [id]
(meetup-layout
[:div {:style "border: 1px dashed #363636"} "TODO - meetup details widget here"]))
(dyna-routes/defpage "/i/:id" [id]
(meetup-layout
[:div {:style "border: 1px dashed #363636"} "TODO - invitation details widget here"]))
The ganelon.web.dyna-routes/defpage macro simply creates compojure.core/ANY route and adds it to a list maintained in the ganelon.web.dyna-routes namespace. The routes can be grouped for easier management and middleware can be defined in a similar way. More information is available on the Routing page of Ganelon project website.
Running the project
After that, we have two general ways to run our project and there is nothing specific to Ganelon about them:
The simplest is to use lein-ring plugin and run lein ring server:
$ lein ring server 2013-08-19 21:39:41.474:INFO:oejs.Server:jetty-7.6.1.v20120215 2013-08-19 21:39:41.502:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:3000 Started server on port 3000
We can also use lein ring uberjar to obtain an executable jar file.
If we are using some IDE not compliant with Leiningen - for example IntelliJ IDEA, we can create a simple script to run our jetty-ring adapter:
(ns run
(:require [ganelon.tutorial]
[ring.adapter.jetty :as jetty]))
(defonce SERVER (atom nil))
(defn start-demo [port]
(jetty/run-jetty ganelon.tutorial/handler {:port port :join? false}))
(ganelon.tutorial/initialize)
(let [port (Integer. (get (System/getenv) "PORT" "3000"))]
(swap! SERVER (fn [s] (when s (.stop s)) (start-demo port))))
This scripts just optionally stops and then starts jetty adapter for our handler - but can be loaded for example into IntelliJ IDEA's REPL.
After applying either method, we can just navigate to http://localhost:3000/ to see the running application.
What's next?
In the next part of the tutorial, I will show how to build widgets, actions and use them to propagate data back and forth between a web app and a MongoDB instance.
Monday, August 19, 2013
Ganelon tutorial, intro
What is Ganelon?
How does Ganelon work?
Ganelon tutorial
- Setup a new Ganelon project, configure basic routes and layout templates (using Hiccup) (PART 1).
- Build reusable, dynamic elements of UI (widgets) and use MongoDB (from mongohq) as a persistence layer (PART 2).
- Add middleware for security handling and define custom operations (PART 3).
- Finally, I will show how can the tutorial app display its source code (PART 4).
Thursday, April 18, 2013
Ganelon 0.9.0 released
In addition to that, an interactive tutorial app (meetings management with MongoDB) has been launched at http://ganelon-tutorial.tomeklipski.com. Source codes for it can be found in GitHub repository: https://github.com/tlipski/ganelon-tutorial.
In the following weeks, this tutorial app will be used as a base for a tutorial blog series.
Using Ganelon
To use Ganelon in your Compojure/Ring/Clojure web application, simply add the following dependency to yourproject.clj file:[ganelon "0.9.0"]For more information on using Ganelon, please visit http://ganelon.tomeklipski.com.
Production use
Most important features
As this is a first major release of Ganelon, instead of changes introduced, I will simply highlight most important features:- AJAX support for client-side operations, including basic functions and almost whole jQuery | Manipulation library.
- Ability to add custom client-side operations.
- Additional libraries for Bootstrap Modal JavaScript and jquery gritter plugin (Growl-style notifications).
- Support for definition of AJAX actions as Compojure routes.
- Basic functions rendering client-side code for HTML/JavaScript controls invoking AJAX actions.
- Support for distributed configuration of Compojure routes - somewhat akin to Noir's
defpagemacro.
Thursday, April 11, 2013
Running and debugging Clojure code with Intellij IDEA
These features make Clojure development with IDEA a real pleasure.
Installation of La Clojure plugin
Importing leiningen project
With the La Clojure plugin ready and active, we can import a leiningen project into IntelliJ IDEA. All we have to do is to generate appropriate Maven pom.xml file:
lein pomThen, we can import the maven module using IntelliJ IDEA.
Step 1: select Import Project from welcome screen:
Starting the REPL
Interacting with REPL
- Run selected text in REPL
- Execute last S-Expression in REPL
- Run top S-Expression in REPL
Debugging with REPL
Step 1: we have to create a Remote Debugger profile using Run / Edit Configurations... from a menu.
To add a Remote Debugger, we have to click on the plus '+' sign and select 'Remote' configuration type:
We can adjust the settings or leave them as default. Most importantly, we have to copy command line arguments for running remote JVM, for example:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005Tuesday, March 19, 2013
Introducing Ganelon - micro-framework supporting AJAX in Clojure/Ring web apps
Ganelon is fully Ring compatible and based on Compojure and lib-noir. Being a AJAX focused, it is not a direct replacement for Noir. It can rather ease the pain of handling dynamic page requests in any Ring-based web application.
The execution model is heavily influenced by Weblocks or Vaadin, but without session-statefulness and out-of-the-box rich user interface:
- Certain parts of the page can be scoped as Widgets (for example using id attribute in HTML or ganelon.web.widgets.with-div utility macro) and referenced by id in Actions, and updated by Operations. Widgets can reference Actions to be invoked.
- Actions are invoked as XHR requests and return a set of Operations to be performed client-side. Actions are in fact simple Ring handlers.
- Operations provide abstraction layer over client-side JavaScript execution - e.g. using Bootstrap Modal or just updating part of DOM tree (a Widget or any other) or open certain URL in a browser window - or just anything else that has a simple integration layer provided.
The source codes are on GitHub: http://github.com/tlipski/
Sample code (a shoutbox) is listed below:
;A Widget, returning HTML code:
(defn box-widget []
(widgets/with-div
[:p "Call count (since restart): " [:b @COUNTER] ". Last 4 entries:"]
(for [entry @ENTRIES]
[:div.hibox [:b (:time entry)] ": "
(hiccup.util/escape-html (:msg entry))])
(widgets/action-form "say-hi" {} {:class "form-inline"}
[:input {:name "msg" :placeholder "Say hi!" :type "text"
:maxlength "20" :size "20"}] " "
[:button {:class "btn btn-primary"} "Send!"])))
;An Action, performing side effects and returning part of the page to be updated
(actions/defwidgetaction "say-hi" [msg]
(swap! COUNTER inc)
(swap! ENTRIES #(util/smart-subvec (flatten [(mkmsg msg) %]) 0 4))
(box-widget))
JavaScript code uses jQuery and optionally Bootstrap, but the implementation is really trivial and therefore easy to replace using your favorite JS library.
Sunday, May 6, 2012
#Clojure , #MongoDB and removing a set of items
When using congomongo library to access MongoDB in Clojure, it is sometimes required to remove a collection of items. As the example on GitHub page shows only how to remove one item, I have pasted the code here.
Setup test data
To setup test data, we would use 100 items in :points collection.
(use 'somnium.congomongo)
(mass-insert! :points
(for [x (range 0 10) y (range 0 10)] {:x x :y y}))
(count (fetch :points))
;=> 100
Please note, that I am not writing any code specific to establishing a connection here. For more information on using congomongo library, please visit congomongo github site.
The obvious way
The most obvious way to do so, would be to pass a list objects to destroy! function. Sadly, it won't work:
user=> (destroy! :points (fetch :points)) ;=> ClassCastException clojure.lang.LazySeq cannot be ;=> cast to com.mongodb.DBObject ;=> somnium.congomongo/destroy! (congomongo.clj:419)
Using for loop
We can also invoke destroy! a hundred times:
(count (fetch :points)) ;=> 100 (for [p (fetch :points)] (destroy! :points p)) (count (fetch :points)) ;=> 0
It will work, but it is not optimal.
Using where clause
The best way to remove a collection of items in MongoDB using congomongo, it would be to use where clause for a destroy! function:
(count (fetch :points))
;=> 100
(destroy! :points {:x {:$gt 4}})
(count (fetch :points))
;=> 50
It is also possible to refer MongoDB items by their ids in where clause:
(count (fetch :points))
;=> 100
(destroy! :points {:_id {:$in (map :_id (fetch :points))}})
(count (fetch :points))
;=> 0

















