我在我的控制器上有一个定义的动作,使模态可见。在我的 hbs模板,我能够创建一个按钮并为其分配动作。当我这样做时,我可以通过单击按钮来显示模态。
我正在尝试显示模态(调用控制器操作)而不必单击任何内容。这可能吗?我想根据模型的条件自动弹出模态。
答案 0 :(得分:1)
这是一个非常简单的方法, 只需创建您想要的模型属性的计算别名
// controller
export default Ember.Controller.extend({
showModal: Ember.computed.alias('model.showModal')
});
// template
{{#if showModal}}
<p>Showing modal</p>
{{/if}}
就是这样
工作示例:https://ember-twiddle.com/3e86c841e9d7ea54d4febd74b5463fb8?openFiles=controllers.application.js%2C
答案 1 :(得分:0)
您可以创建组件,将按钮添加到组件,然后将模型和操作传递到组件中。
然后在组件的static func sendArrival(scan: ArrivalScan){
var urlComponents = URLComponents()
urlComponents.scheme = "https"
urlComponents.host = "api.my.url.com"
urlComponents.path = "/Delivery/Arrival/?id="
guard let url = urlComponents.url else { fatalError("Could not create URL from components") }
// Specify this request as being a POST method
var request = URLRequest(url: url)
request.httpMethod = "POST"
// Make sure that we include headers specifying that our request's HTTP body
// will be JSON encoded
var headers = request.allHTTPHeaderFields ?? [:]
headers["Content-Type"] = "application/json"
headers["ZM_APIKey"] = "mySecretKey"
request.allHTTPHeaderFields = headers
// Now let's encode out Post struct into JSON data...
let encoder = JSONEncoder()
do {
let jsonData = try encoder.encode(scan)
// ... and set our request's HTTP body
request.httpBody = jsonData
print("jsonData: ", String(data: request.httpBody!, encoding: .utf8) ?? "no body data")
} catch {
//TODO: error handling
}
// Create and run a URLSession data task with our JSON encoded POST request
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: request) { (responseData, response, responseError) in
guard responseError == nil else {
//TODO: error handling
return
}
// APIs usually respond with the data you just sent in your POST request
if let data = responseData, let utf8Representation = String(data: data, encoding: .utf8) {
print("response: ", utf8Representation)
} else {
print("no readable data received in response")
}
}
task.resume()
}
生命周期钩子中,进行模型条件检查并调用传入的操作(如果它是init
。
true
据我所知,控制器中没有生命周期钩子,每次访问特定页面时都会触发。将检查添加到组件可确保每次访问页面时都会触发检查,因为它始终会呈现按钮组件。