javascript

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package javascript converts go objects and types to javascript code, suitable for embedding in html or sending to the browser via a specialized ajax call.

Index

Examples

Constants

View Source
const JsonObjectType = "goraddObject"

JsonObjectType is used by the ajax processor in goradd.js to indicate that we are sending a special kind of object to the browser. These are things like dates, closures, etc. that are not easily represented by JSON.

Variables

This section is empty.

Functions

func Closure

func Closure(body string, args ...string) closure

Closure represents a javascript function pointer that can be called by javascript at a later time.

Example
c := Closure("return a == b;", "a", "b")
fmt.Println(c.JavaScript())
Output:

function(a, b) {return a == b;}

func ClosureCall

func ClosureCall(body string, context string, args ...string) closureCall

ClosureCall represents the result of a javascript closure that is called immediately context will become the "this" variable inside the closure when called.

Example
c := ClosureCall("return this == b;", "a", "b")
fmt.Println(c.JavaScript())
Output:

(function(b) {return this == b;}).call(a)

func Function

func Function(name string, context string, args ...interface{}) functionCall

Function represents the result of a function call to a global function or function in an object referenced from global space. The purpose of this is to immediately use the results of the function call, as opposed to a Closure, which stores a pointer to a function that is used later. context will become the "this" value inside the closure args will be passed as values, and strings will be quoted. To pass a variable name, wrap the name with a JsCode call.

func JsCode

func JsCode(s string) jsCode

JsCode represents straight javascript code that should not be escaped or quoted. Normally, string values would be quoted. This outputs a string without quoting or escaping.

func NoQuoteKey

func NoQuoteKey(v interface{}) noQuoteKey

NoQuoteKey is a value wrapper to specify a value in a map whose key should not be quoted when converting to javascript. In some situations, a quoted key has a different meaning from a non-quoted key. For example, when making a list of parameters to pass when calling the a javascript command, quoted words are turned into parameters, and non-quoted words are turned into functions. For example, "size" will set the size attribute of the object, and size (no quotes), will call the size() function on the object. i.e. map[string]string {"size":4, "size":NoQuoteKey(JsCode("obj"))}

func NumberFloat

func NumberFloat(i interface{}) float64

NumberFloat is a helper function to convert an expected float that is returned from a json Unmarshal as a Number, into an actual float64 without returning any errors. If there is an error, it just returns 0. Use this when you absolutely know you are expecting a float. Can convert strings too.

func NumberInt

func NumberInt(i interface{}) int

NumberInt is a helper function to convert an expected integer that is returned from a json Unmarshal as a Number, into an actual integer without returning any errors. If there is an error, it just returns 0. Use this when you absolutely know you are expecting an integer. Can convert strings too.

func NumberString

func NumberString(i interface{}) string

NumberString is a helper function to convert a value that might get cast as a Json Number into a string. If there is an error, it just returns 0. Use this when you absolutely know you are expecting a string.

func ToJavaScript

func ToJavaScript(v interface{}) string

ToJavaScript will convert the given value to javascript such that it can be embedded in a browser. If it can, it will use the JavaScripter interface to do the conversion. Otherwise it generally follows json encoding rules. Strings are escaped. Nil pointers become null objects. String maps become javascript objects. To convert a fairly complex object, like a map or slice of objects, convert the inner objects to interfaces

Types

type Arguments

type Arguments []interface{}

Arguments represents a list of javascript function arguments. We can output this as javascript, or as JSON, which gets sent to the goradd javascript during Ajax calls and unpacked there. Primitive types get expressed as constant values in javascript. If you want to represent the name of variable, us a JsCode object. Function can be represented using the Function object or the Closure object, depending on whether you want the output of the function now, or later.

func (Arguments) JavaScript

func (a Arguments) JavaScript() string

JavaScript returns the arguments as a comma separated list of values suitable to put in JavaScript function arguments.

type JavaScripter

type JavaScripter interface {
	JavaScript() string
}

JavaScripter specifies that an object can be converted to javascript (not JSON!). These objects should also be gob encodable and registered with gob, since they might be embedded in a control and need to be serialized.

type Undefined

type Undefined struct {
}

Undefined explicitly outputs as "undefined" in javascript. Generally, nil pointers become "null" in javascript, so use this if you would rather have an undefined value.

func (Undefined) JavaScript

func (n Undefined) JavaScript() string

func (Undefined) MarshalJSON

func (n Undefined) MarshalJSON() ([]byte, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL