Durandal Framework

repository·master·Indexed 23 days ago

https://github.com/bluespire/durandal

A cross-device, cross-platform JavaScript client framework for building Single Page Applications (SPAs) for PC, Mac, Linux, iOS, and Android. Durandal utilizes a composition-based model to bind View Models to Views, leveraging core dependencies including RequireJS, Knockout, jQuery, and Bootstrap. It provides a consistent API for SPA development using Promises for asynchronous operations and a router for managing navigation state.

Tokens
50.9K
Snippets
103
Records
299
Agent score
83%

What's inside Durandal

  1. Overview of Durandal SPA Framework

    master
    Durandal is a lightweight JavaScript framework designed for building Single Page Applications (SPAs). It leverages existing libraries like jQuery, Knockout, and RequireJS to provide a familiar development experience without reinventing core web technologies. It is designed to be platform-agnostic, supporting web deployment, mobile deployment (via PhoneGap), and desktop deployment (via AppJS).
  2. Overview of Durandal

    master

    Durandal is a JavaScript-based client framework designed for building cross-device and cross-platform Single Page Applications (SPAs). It is intended to simplify the creation and maintenance of applications targeting PC, Mac, Linux, iOS, and Android.

    Note: This project is no longer active and has been superseded by Aurelia.

  3. Project Structure in the Durandal Starter Kit

    master

    When working with the Durandal Starter Kit, the project is organized into the following directories and files:

    • App/: Contains the main application code, organized into AMD modules and HTML views.
    • App/main.js: The entry point for application startup.
    • Content/: Contains CSS, fonts, and images.
    • Scripts/: Contains third-party script libraries.
    • App_Start/DurandalBundleConfig.cs: Configuration for CSS and script library bundling.
    • Durandal/Index: The host page containing the base HTML, links, and script references.
  4. What is an Overridable Designation in Durandal

    master
    In the Durandal documentation, certain functions are marked with the [overridable] indicator. This designation signals that the function is a deliberate extension point designed to be replaced by your own implementation to customize framework behavior. Replacing an overridable function is a supported way to extend Durandal and is intended to be safe from negative side-effects.
  5. What is composition in Durandal

    master

    Durandal uses two types of composition to build applications:

    1. Object Composition: Achieved via AMD/RequireJS. Modules can depend on other modules (e.g., Module B requires Module A), allowing for modularization of logic.
    2. Visual Composition: Achieved via the compose binding. This allows developers to break views into reusable components and connect them to their corresponding viewmodels (modules). This is the central mechanism for rendering the UI, including widgets and modal dialogs.
  6. How views and modules work together in Durandal

    master

    Durandal uses a module-view pattern where modules (JavaScript files) provide the logic and data, and views (HTML files) provide the structure.

    Key principles:

    • Pairing: Modules and views are paired together by convention (e.g., customerList.js pairs with customerList.html). This pairing is managed by the view locator.
    • Data Binding: Use Knockout.js bindings within the HTML view to synchronize the DOM with the module's properties. When a property in the module changes, the DOM updates automatically, and user interactions in the DOM can update the module.
    • Root Element: Every view must have exactly one root element. If multiple root elements are present, Durandal will wrap them in a <div>. If comments are found at the root, they will be removed.
  7. Use the compose binding for navigation

    master

    The compose binding is a powerful Knockout-based feature used to swap views dynamically. In a shell/layout, you can bind the compose instruction to an observable (like a router's active item). When the observable changes, Durandal:

    1. Detects the change.
    2. Uses the viewLocator to find the appropriate view for the new module.
    3. Data-binds the new view and model together.
    4. Inserts the result into the DOM at the binding location.
    5. Applies an optional transition animation.

    Syntax:

    <!--ko compose: { 
        model: activeItemObservable,
        afterCompose: callback,
        transition: 'animationName'
    }--><!--/ko-->
    <!--ko compose: { 
        model: router.activeItem,
        afterCompose: router.afterCompose,
        transition:'entrance'
    }--><!--/ko-->
  8. Use Knockout binding handlers to encapsulate view logic

    master
    The most common way to interact with the DOM in Durandal is by creating custom Knockout (KO) binding handlers. This allows you to encapsulate view-related code (like wrapping jQuery plugins) and keep your modules free of direct DOM manipulation. Durandal's own composition and widget bindings are implemented as custom KO binding handlers.
  9. Manage navigation and screen state lifecycle

    master

    Durandal's router and lifecycle management handle complex navigation scenarios:

    Routing

    The router supports:

    • Mappings: Route-to-module mappings or convention-based routing.
    • Navigation Modes: Support for both hash and pushState navigation.
    • Data-Driven Routes: Support for parameters, optional parameters, splats, and query strings.
    • Child Routers: Ability to encapsulate entire areas of an application with their own routing structures.

    Screen Activation Lifecycle

    To handle 'dirty' states (e.g., preventing a user from navigating away from an unsaved form), Durandal implements a screen activation lifecycle. This allows any screen, fragment, or component to synchronously or asynchronously control the flow into and out of a navigation event. This lifecycle is also used to manage the behavior of modal dialogs.

  10. Handle asynchronous operations with Promises

    master

    Durandal is designed around Promises to handle asynchronous data requests and resource loading.

    • API Consistency: All potentially asynchronous Durandal APIs return promises.
    • Promise Libraries: While jQuery provides the default promise implementation, you can switch this out for other libraries like Q.
    • Observable Module: When targeting ES5 browsers, enabling the observable module allows the Knockout binding system to data-bind directly to promises. For example, you can use a foreach binding directly on a promise that resolves to an array of data, removing the need to manually resolve the promise in your application logic.