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 ¶
- Constants
- func Closure(body string, args ...string) closure
- func ClosureCall(body string, context string, args ...string) closureCall
- func Function(name string, context string, args ...interface{}) functionCall
- func JsCode(s string) jsCode
- func NoQuoteKey(v interface{}) noQuoteKey
- func NumberFloat(i interface{}) float64
- func NumberInt(i interface{}) int
- func NumberString(i interface{}) string
- func ToJavaScript(v interface{}) string
- type Arguments
- type JavaScripter
- type Undefined
Examples ¶
Constants ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.