Citestore Plugin

Hi, apologies in advance if I’m dev-splaining anything.

The citestore.js.es6 file is included in the javascript application is sent to your users’ computers when they load up Discourse.

The plugin.rb code lives on the server which is running Discourse.

The way those two things communicate is through API requests. So, for instance, if I load this topic in meta, there’s a request from my computer to the server, requesting information about the topic. You can see what network requests are being made by visiting the ‘Network’ tab in chrome:


(that’s a picture of me requesting the first post in topic 51620. There are many like it, but this one is yours. Note that I’ve filtered out most of the other requests by typing ‘/t/’ into the filter box up top.)

On the client side, you can initiate a request using the ajax library:

import { ajax } from 'discourse/lib/ajax'
getThing() {
  ajax('/citestores/us-constitution-1', function(data) {
    // in here, you can use the data you've fetched!
  })
}

On the server side, you’ll need to set up a route and a controller to respond with some data when that request is initiated

  Discourse::Application.routes.append do
    get '/citestores/:key', to: 'citesstores#show'
  end
CitestoresController < ApplicationController
  def show
    @citestore = Citestore::Storage.get(params[:key])
    respond_with json: { handle: @citestore.handle, locus: @citestore.locus } 
  end
end

NB: this code is an untested approximation, but should at least get you towards communicating between client and server. :smiley:

5 Likes