npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@profiscience/knockout-contrib-model

v1.0.0-alpha.45

Published

An opinionated model with first-class TypeScript support and router integration with KnockoutJS

Downloads

5

Readme

@profiscience/knockout-contrib-model

Version Dependency Status Peer Dependency Status Dev Dependency Status Downloads

NOTE: Assumes a rudimentary understanding of KnockoutJS and Webpack (or the bundler du jour).

Composable view and data model builders for KnockoutJS with @profiscience/knockout-contrib-router integration designed with the following goals in mind...

User Experience

No Jarring

There are two main schools of thought on loaders and SPA navigation. The first is is to allow each view/component (and subview in nested routing) to manage itself. This yields a UX similar to Facebook, where parts of the page come in at different times. There is nothing wrong with this approach, but it requires more code, and more care to be taken in constraining dimensions in CSS so that the page doesn't jar — in many cases it's impossible to prevent jarring.

The alternative is the approach that is used here, which is to display a global loader and show that until the next page is completely ready, and then render it. No view is rendered until all of its data has been loaded.

GitHub uses this approach, and IMHO it is an optimal UX.

Lazy Loaded

Minimal code should be required for initial page load, as well as navigation.

Developer Experience

First-Class TypeScript Support

When following best-practices, types should be intuited as strictly as possible — i.e. noImplicitAny compatibility. TypeScript usage is henceforth assumed.

Minimal Logic Boilerplate

Boilerplate should be declarative and mostly confined to defining types.

Loading async data should require no extra effort additional code. i.e. there should be no need to wrap component templates with if: ready and create custom CSS to prevent jarring. Nor should a developer have to create middleware by hand for each route.

Reusability

Heed to DRY principles. Prevent repetition wherever possible.

Testability

Unit testing views should be trivial. TDD should help a developer, not hurt.

Extensibility

Easily allow extending, taking care to avoid conflicts and prevent leaks by using Symbols where applicable.

API

@TODO

import { DataModelConstructorBuilder, ViewModelConstructorBuilder } from '@profiscience/knockout-contrib-model'

Usage

app.js

import * as ko from 'knockout'
import { Route, Router } from '@profiscience/knockout-contrib-router'
import { componentPlugin, dataPlugin } from '@profiscience/knockout-contrib-router-plugins'

const home = new Route('/', {
  // via router.plugins.component package
  component: () => ({
    template: import('./template.html'),
    viewModel: import('./viewModel')
  })
})

Router
  .useRoutes(home)

Route
  // ** REQUIRED **
  .usePlugin(componentPlugin)

  // Must be registered ** AFTER ** component plugin. Delays render until all
  // DataModel properties on the viewModel instance have been initialized.
  .usePlugin(dataPlugin)

ko.applyBindings()

viewModel.js

import { ViewModelConstructorBuilder } from '@profiscience/knockout-contrib-model'
import { DataModel } from './dataModel'

export default class ViewModel extends ViewModelConstructorBuilder {
  public data = new DataModel()
}

dataModel.js

import { DataModelConstructorBuilder } from '@profiscience/knockout-contrib-model'

export interface IDataModelParams {}

export default class DataModel extends DataModelConstructorBuilder<IDataModelParams{
  public name: string

  public async fetch() {
    // assume this returns { name: 'Casey' }, as defined above
    return await $.get('https://example.com/api/name')
  }
}

template.html

Hello, <span data-bind="text: data.name"></span>

Walking through this code step-by-step, the following occurs...

  • Page is loaded on the home url (/)
  • Router loads the component files (viewModel.js and template.js)
  • componentPlugin creates a viewModel instance and registers component with { viewModel: { instance } }
  • dataPlugin looks for DataModel instances on the viewModel instance; if found, prevents render until initialized
  • Router completes render

Contents