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

rc-model

v2.0.4

Published

A JAVASCRIPT LIBRARY FOR BUILDING DATA-DRIVEN REACT APPLICATIONS

Downloads

2

Readme

React Model

npm version Travis build status npm download npm license

A JAVASCRIPT LIBRARY FOR BUILDING DATA-DRIVEN REACT APPLICATIONS

As we know, React has no networking/AJAX features. And you can see in this article: http://andrewhfarmer.com/react-ajax-best-practices/, we have many approach to make an AJAX request in React app.

I like Relay, especially Higher-Order Components pattern but it only works with GraphQL. Then I make a small library based on Relay concept and it works with RESTful backends.

Installation

You'll need both React and React Model:

rc-lazy

Containers

Create a React component with Store decorator:

import React, { Component } from 'react'
import { Xhr, MutationType, Store } from 'rc-model'

Xhr.BASE_URL = '/api'

@Store({
  endpoint: 'system'
})
class MyComponent extends Component {
  render() {
    const { TestData } = this.props.store.data
    return <div>
      <p>{JSON.stringify(TestData)}</p>
    </div>;
  }
}

export default MyComponent

You can setup network layer throught Xhr.BASE_URL. In this case, when you setup Xhr.BASE_URL = '/api', every AJAX requests will call to http://<IP Server>:<Port>/api/

You need to setup the endpoint to make an AJAX request to http://<IP Server>:<Port>/api/<endpoint> throught Container decorator:

@Store({
  endpoint: 'system'
})

The response data will be pushed to this.props.store.data.

In case you want to add some query params, just change the endpoint fragment:

@Store({
  endpoint: {
    name: 'system',
    initialVariables: () => {
      return {
        page: 1,
        size: 20
      }
    }
  }
})

If you want to do something with the response data before it's pushed to props, just add the next function like below:

@Store({
  endpoint: 'system',
  next: response => {
    // Do something with response before return
    return response
  }
})

Mutations

Add mutations fragment into Container decorator, type can be MutationType.POST, MutationType.PUT, MutationType.DELETE.

@Store({
  endpoint: 'system',
  mutations: {
    login: {
      type: MutationType.POST,
      path: 'security/login'
    }
  }
})

As you can see from above, we add login to mutations fragment as a POST request and it will call to http://<IP Server>:<Port>/api/security/login Then you can call login as a function like this:

  handleLogin() {
    this.props.store.login({
      record: {
        Id: "my_id",
        Password: "my_password"
      },
      next: response => {
        console.log(response)
      },
      error: () => {
        console.error('Error!!!')
      }
    })
  }

We pass an object as a parameter into login function with the format:

{
  record:  { ... }, // data to be sent to the server
  next: response => { ... }, // a function to be called if the request succeeds.
  error: response => { ... } // a function to be called if the request fails.
  complete: response => { ... } // a function to be called if the request fails.
}

Handle when Ajax complete

You can register a handler to be called when Ajax requests complete (with an error):

import { Xhr } from 'rc-model'

Xhr.ajaxBefore = () => {
  console.log('Ajax Before')
}

Xhr.ajaxError = (error) => {
  console.log('Ajax Error!!!')
}

Xhr.ajaxComplete = () => {
  console.log('Ajax Complete')
}

License

rc-model is released under the MIT license.